[ACCEPTED]-How to consume WCF web service through URL at run time?-wcf-client
In order to use a WCF service, you will 23 need to create a WCF client proxy.
In Visual 22 Studio, you would right-click on the project 21 and pick the "Add Service Reference" from 20 the context menu. Type in the URL you want 19 to connect to, and if that service is running, you 18 should get a client proxy file generated 17 for you.
This file will typically contain 16 a class called MyServiceClient - you can instantiate 15 that class, and you should see all the available 14 methods on that client class at your disposal.
If 13 you don't want to add a service reference 12 in Visual Studio, you can achieve the same 11 result by executing the svcutil.exe
command line tool 10 - this will also generate all the necessary 9 files for your client proxy class for you.
Marc
UPDATE:
if 8 you want to initialize a client proxy at 7 runtime, you can definitely do that - you'll 6 need to decide which binding to use (transport 5 protocol), and which address to connect 4 to, and then you can do:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8888/MyService");
MyServiceClient serviceClient = new MyServiceClient(binding, address);
But even in this 3 case, you need to have imported and created 2 the proxy client first, by using the "Add 1 Service Reference" or svcutil.exe tools.
To answer how to do it without having a 12 service reference. Have a look here (option 11 #a):
You still need some reference (namely 10 a reference to an assembly containing the 9 contract / interface) but you do not make 8 a service reference.
EDIT: Though the above is 7 possible I would not recommend it. Performance 6 is not exactly great when you have to generate 5 the proxies like this. I usually use svcutil.exe 4 and create an assembly containing my clients 3 and create a reference to that assembly. This 2 way you have more options for controlling 1 what the proxies look like.
You can also make use of the WebClient class to call 8 the WCF service without needing a service 7 proxy. Effectively you can send and receive 6 Strings and Binary data and also simulate 5 POSTs.
I use it extensively for reusable 4 components where the developer may not ever 3 create the required proxy methods. A good 2 comparison of ways to do POST is available 1 here.
You call it with a /functionname, eg:
http://localhost/MyService/MyService.svc/GetVersionNumber
Edit:
How do you 5 configure your method in the WCF service 4 so you can call it directly from the browser?
I 3 have an Interface:
[ServiceContract]
public interface IWebServiceImpl
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetVersionNumber")]
string GetVersionNumber();
And a class to implement 2 the GetVersionNumber
method in the Interface:
public class WebServiceImpl
{
public string GetVersionNumber()
{
return "1.0.0.0"; //In real life this isn't hard-coded
}
}
Finally here 1 is the Web.config configuration:
<system.serviceModel>
<diagnostics>
<messageLogging logEntireMessage="true" logMalformedMessages="false" logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="false" maxMessagesToLog="3000" maxSizeOfMessageToLog="2000"/>
</diagnostics>
<bindings>
<webHttpBinding>
<binding name="webBinding">
<security mode="Transport"/>
</binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="YOURWebServiceNameSpace.WebServiceImpl">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="YOURWebServiceNameSpace.IWebServiceImpl"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
You can just provide the wsdl of your service: http://localhost/MyService/MyService.svc?wsdl.
From 2 wsdl you can generate proxy classes and 1 use them on the client.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.