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"?
Related
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.
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>
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
We run a web application with many (50+) WCF service hosts (Written in C#, running on Win 2008R2/IIS 7.5) for integration with different external vendors. We would like to add another service to each of these hosts for administration. This service can be generic enough that we have a single service definition which we define in some library which we can reference from the integration projects. But is there a way of actually adding the endpoint only in configuration? That is, I would like to avoid adding a .svc file, and only add some rows in the web.config service-section.
I tried adding this to the web.config:
<service name="Contracts.AdminService">
<endpoint address="AdminService" contract="Contracts.IAdminService" binding="basicHttpBinding" />
</service>
This does not work however. If I add a simple .svc file like below it works
<%# ServiceHost Language="C#" Debug="true" Service="Contracts.AdminService" CodeBehind="Contracts.AdminService.cs" %>
However, I would really like to avoid this so that it is less likely that a mistake is made when setting up a new host (we already do a bunch of automatic stuff with the config file, so adding a new service would be trivial). Is this possible? Any drawbacks?
Found the answer myself after a while. The solution is WCF 4's file-less activation. This let me just add this to my web.config:
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true">
<serviceActivations>
<add relativeAddress="AdminService.svc" service="Contracts.AdminService"/>
</serviceActivations>
</serviceHostingEnvironment>
After this I can access the service by going to http://<hostname>/<path>/AdminService.svc.
WCF services are hosted in a ServiceHost.
ServiceHosts must be manually created when you are not hosting your WCFs in IIS/WAS, as both IIS/WAS interact with the ServiceHost on your behalf.
What you are basically doing inside the .svc file in IIS is instantianting a new ServiceHost.
Because ServiceHost can only host a single service type, if you plan on creating new types of services for adminsitration, you will need a service host for each of those new types.
So, the short answer to your questions is that you can't just add some information in your web.config for the new services you are going to create, you also need a mechanism to instantiate a ServiceHost.
My customer gave me a .wsdl file to access their webservices. Using VS2008 I can create a project, but I don't know how to use the .wsdl file in it.
You don't invoke WSDL file, you add service reference from the file.
To add reference, right click on the project, select Add Service Reference. Paste path to your wsdl file and hit Go.
If you want to use legacy Web Service client, select Add Web Reference and paste path to the wsdl file from there.
I recommend to use WCF (Add Service Reference option).
To use the service reference add code like this:
var serviceClient = new ServiceReferenceName.MyClassClient();
serviceClient.DoSomething();
You also need to update config file with the server URL that you customer should provide you with:
<client>
<endpoint address="http://UrlFromYourCustomerHere"
binding="basicHttpBinding"
bindingConfiguration="xxx"
contract="MyServiceReference.xxx"
name="xxx/>
</client>
A Web reference enables a project to
consume one or more XML Web services.
Use the Add Web Reference Dialog Box
to search for Web services locally, on
a local area network, or on the
Internet.
After adding a Web reference to your
current project, you can call any
methods exposed by the Web service.
To add a Web Reference
On the Project menu, click Add Web Reference.
In the URL box of the Add Web Reference dialog box, type the URL to obtain the service description of the Excel Web Services, such as http:////_vti_bin/excelservice.asmx or http:///_vti_bin/excelservice.asmx. Then click Go to retrieve information about the Web service.
Note Note:
You can also open the Add Web Reference dialog box in the Solution Explorer pane by right-clicking References and selecting Add Web Reference.
In the Web reference name box, rename the Web reference to ExcelWebService.
Click Add Reference to add a Web reference for the target Web service.
Visual Studio downloads the service description and generates a proxy class to interface between your application and Excel Web Services.
Read
How to: Add and Remove Web References