Add header information to my Web Service Reference request - c#

I want to add header information to my Web Service, which I created with basic wizard in Visual Studio 2012.
I simply clicked "Add Service Reference", gave my link and VS created the code itself. Which class do I have to use to pass the header information of my request? Do I have to override the code that VS created automatically?
One more note: If you know another way to add a header to a web service, I would like to use it too. I don't have to use automaticaly created codes. (Although it's handy to use them.)

.Net Framework creates and patches the header on its own in the background, so what I should be doing is to add the username-password header values on web.config, instead of binding it to the post itself.
<client>
<endpoint address="http://ServiceAdd/FService" binding="basicHttpBinding"
bindingConfiguration="FServiceSoapBinding" contract="MyReference.MService_"
name="FServicePort" >
<headers>
<Account>
<username>user</username>
<password>password</password>
</Account>
</headers>
</endpoint>
</client>

Related

Debug SVC WCF and ASP.NET Solution

I'm currently working on an ASP.NET MVC 4.5 application. I'm new to that project as well as WCF and look for a way to properly debug the project using regular breakpoints.
Given are 2 solutions:
ASP.NET Project in: Solution 'Projects'
WCF Project in: Solution 'ProjectService'
in my 1st solution Solution 'Projects' I'm calling the WCF Service the following way:
OperationResult<FindProjectsResult> projectsResult = _projectsWcfService.Call(p => p.FindProjects())
and reference the Wcf in the Web.config file:
<client>
<endpoint address="/ProjectSearch.svc" binding="NamedPipeBinding" name="ProjectServiceNetNamedPipe">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
The implementation for my Wcf Service is in the 2nd solution Solution 'ProjectService' in my svc file ProjectSearch.svc.cs.
The method I'm calling from solution 1 looks like that:
public OperationResult<FindProjectsResponse> FindProjects()
{
return ProjectManagement.FindProjects();
}
It all works fine.
I need to know, how to debug the WCF Service.
Currently I'm attaching solution 1 to my w3wp.exe process. Though I cannot attach 2 solutions on 1 process.
I would be fine with any help on how to debug the WCF service.
Thanks a lot!!!
Right-click on the solution, you can configure your solution so that you can debug multiple projects via visual studio.

How can I use a web service from local WSDL file?

I have been given a WSDL file from a provider and I have added it as a Service Reference to my C# project in Visual Studio 2013.
I can see the relevant classes I need but when I call the functions on them nothing is transmitting from my program. I have configured Wireshark to listen but no data is coming from my program when I run it.
Where in Visual Studio can I see the IP address/URL that the web service is trying to connect to? At some point I assume it establishes a HTTP connection, where can I see this code to check the URL/IP address?
The WSDL file does not contain the address of the service endpoint. You probably created a Service Reference or a Web Reference, which has created a client class for you. If you instantiate this client (lets call it ExampleClient) with the default constructor:
var serviceCLient = new ExampleClient();
Then the URI will be the path of the WSDL file you imported. This mostly works fine if you import a generated WSDL file file the actual service URI, but in your case, you need to tell it where the service is running. You can either pass the service URI in the constructor:
var serviceCLient = new ExampleClient("http://example.com/service/endpoint");
Or edit your app.config or web.config (depending on project type). It will have something like this:
<system.serviceModel>
<client>
<endpoint address="C:\path\to\your.wsdl" etc etc etc... />
</client>
</system.serviceModel>
And you should change the address attribute there.
For old style web references, you can right click on the reference in Visual Studio (under your project in the Web References folder, and select "properties". The properties screen contains a "Web Reference Url" which you can edit to point to the actual service URI.

Include WebService app.config settings in code

my code includes a reference to a webservice, if I want it to run, I need the following section in my app.config:
<client>
<endpoint address="http://123.45.6.78:8080/ASPPO/StartASPPOCallBack"
binding="basicHttpBinding" bindingConfiguration="StartASPPOCallBackPortBinding"
contract="ASPPOCallBack.StartASPPOCallBack" name="StartASPPOCallBackPort" />
</client>
My problem is now, that my code gets called by a SSIS-package and this doesn't have any app.config. So I somehow need to include all the settings I need directy in the code, because I will get a System.Reflection.TargetInvocationException otherwise.
But I actually have no idea where to put it. Can you give me a hint?
If your code is invoked by SSIS (not via external app) you have to modyfy .config file of application which will execute your SSIS package: DTExec.exe, dtshost.exe, devenv.exe, DTExecUI.exe. Here you have more info of appropriate .config locactions: http://www.sqlis.com/post/Where-is-my-appconfig-for-SSIS.aspx
Alternatively you can configure your endpoint in code (not via .config file) similar to http://blogs.msdn.com/b/dbrowne/archive/2010/07/08/how-to-configure-an-ssis-package-to-access-a-web-service-using-wcf.aspx

How to extract the endpoint url when adding service references?

When added a service reference in vs.net 2008, the url to the wcf service is hardcoded in the generated files.
How can I extract this out to my web.config so I can potentially change the url?
The service reference should automatically generate configuration data, including the endpoint, into your web.config. The URL will be inside the client configuration:
<endpoint address="http://localhost:8732/Service.svc/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService"
contract="Service.IService" name="WSHttpBinding_IService" />
When creating a client instance, you also have the option to override the endpoint address in one of the overloaded constructors.
Nothing should be hard-coded in the generated files. Are you sure you're adding a "service reference" and not an old-style "web reference"?

WSDL Generator for C#

Does anyone know of a good tool to generate the WSDL for a service contract written in C# (i.e. set of methods that are tagged as "[OperationContract]" using WCF)? All the tools I've found work the other way around: create code stubs from a WSDL. I don't want to have to hand-jam a WSDL file. I've found tools for php and J2EE, but not C#. Thanks!
svcutil or just host it quickly and hit the MEX point :)
Easiest thing to do is host the service with a base address setup, and then just hit it from a browser with "?wsdl" appended to the end.
Here's an example of a service configuration with a base address specified. Note this goes in the <configuration><services> element in your config:
<service name="MyServiceName" behaviorConfiguration="MyServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:9000/MyService"/>
</baseAddresses>
</host>
<endpoint address="net.tcp://localhost:9001/MyService"
binding="netTcpBinding"
contract="IMyService"
bindingConfiguration="MyServiceBinding"/>
</service>
Once you get it hosted, just go to http://localhost:9000/MyService?wsdl to see the WSDL definition.
Two ways:
a) Download wsdl file and do below steps:
i) Open visual studio command prompt as an administrator.
ii) Type below command:
wsdl.exe [path To Your WSDL File]
b) With endpoint:
i) Open Visual studio command prompt as an administrator.
ii) type below command:
wsdl.exe http://localhost:9000/MyService

Categories

Resources