How to use svcutil.exe to create client proxy ib WCF? - wcf

I have written a wcf service which is working fine.
On the client side i am using AddServiceReference feature of VS2008 to generate client proxy, it's also working fine.
Now, for learning purpose i want to generate my client proxy with svcutil.exe tool but i am not able to use it, i have tried but don't know what is missing maybe i am missing some parameters, i know that AddServiceReference feature also uses svcutil.exe to generate proxy at client side.
Anyone please tell me how to use.
Actually my wcf service project is located at C:\Projects\WCFService and my client is at
C:\Projects\WCFClient.
Below is my service class...
Uri address = new Uri("http://localhost:8090/MathServices/");
using (ServiceHost host = new ServiceHost(typeof(MathOperations), address))
{
BasicHttpBinding binding = new BasicHttpBinding();
host.AddServiceEndpoint(typeof(IMathOperations), binding, "");
ServiceMetadataBehavior metaDataBehavior = new ServiceMetadataBehavior();
metaDataBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(metaDataBehavior);
host.Open();
}

We have found that it is better to use WCF without the autogenerated proxies.
Have a look at this video:
http://www.dnrtv.com/default.aspx?showNum=103
Note: This only works if you have control of both the server and the client side.

Use svcutil YourServiceUrl to generate the proxy class.
If your datacontract contains lists, use /ct:System.Collections.Generic.List1` or else the proxy class will have arrays instead of lists.

Related

How to prevent WCF proxy from following redirect?

I'm using basicHttpBinding for my WCF service and have a message inspector that sets response code to Redirect under certain circumstances. I find that the WCF proxy (generated by svcutil) automatically tries to follow the redirect. How do I prevent this from happening?
Thanks,
Priya
Can you reference the service contract assembly from your client application? If so you can get rid of the generated service reference and just spin up a proxy at runtime using ChannelFactory.
For example:
// Create service proxy on the fly
var factory = new ChannelFactory<IMyServiceContract>("NameOfMyClientEndpointInConfigFile");
var proxy = factory.CreateChannel();
// Create data contract
var requestDataContract = new MyRequestType();
// Call service operation.
MyResponseType responseDataContract = proxy.MyServiceOperation(requestDataContract);
In the above example, IMyServiceContract is your service contract, and MyRequestType and MyResponseType are your data contracts, which you can use by referencing the assembly which the service also references (which defines these types).
what have you tried to achieve with redirect ? you will an handle this cases with some message interceptors on client side:
http://msdn.microsoft.com/en-us/library/ms733786(v=vs.90).aspx

Set ClientCredentials without using a Proxy?

Is it possible to set ClientCredentials without using a generated proxy? I have seen something about using a ChannelFactory, but is it possible to do it without this as well?
In order to call a WCF service from a client, you need a proxy (unless you want to hardcode the SOAP request yourself, which isn't something too easy to do, especially if you're dealing with security). The proxy can be created either using one of the tools to generate the proxy (Add Service Reference, svcutil, etc), or by using ChannelFactory<T>. If you're using a generated proxy, you'd use the ClientCredentials property of the proxy (inherited from the base class ClientBase<T>. If you use the ChannelFactory<T>, you'd set them in the Credentials property of the channel factory.
Here is the sample code:
EndpointAddress endpointAddress = new EndpointAddress("http://localhost:8888/TestSevice");
WSHttpBinding wsHttpBinding = new WSHttpBinding();
ChannelFactory<ISomeServiceInterface> iFactory = new ChannelFactory<ISomeServiceInterface>(wsHttpBinding, endpointAddress);
var clientCredentials = new ClientCredentials();
clientCredentials.UserName.UserName = "sudipto";
clientCredentials.UserName.Password = "sudipto";
iFactory.Endpoint.Behaviors.RemoveAll<ClientCredentials>();
iFactory.Endpoint.Behaviors.Add(clientCredentials);
var isomeService= iFactory.CreateChannel();
isomeService.SomeFunctionToCall("parameterToTheService");

Unable to set CookieContainer on service client in MonoTouch

I have a MonoTouch project using some code I share with a Windows Phone 7 app. This shared code creates a WCF proxy for a RIA Domain Service (using the /Soap endpoint), generated using SLSvcUtil.exe. This is what the code looks like:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://someurl/someservice.svc");
var client = new MyDomainServiceSoapClient(binding, address);
client.CookieContainer = _cookieContainer; // <-- EXCEPTION here
This piece of code works in WP7, but fails in MonoTouch, and I can't find why. The exception I get in MonoTouch is:
System.InvalidOperationException: Unable to set the CookieContainer.
Please make sure the binding contains an HttpCookieContainerBindingElement.
at MyDomainServiceSoapClient.set_CookieContainer
I have tried the following options before setting the CookieContainer, but still the same error:
binding.EnableHttpCookieContainer = true;
binding.AllowCookies = true;
binding.CreateBindingElements()
.Add(new HttpCookieContainerBindingElement()); // ??
Update: I have also tried building a CustomBinding by hand and adding an HttpCookieContainerBindingElement but this also won't work.
Any ideas? The MonoTouch site tells me that the WCF implementation is "experimental", so maybe this is a limitation in the current version of MonoTouch?
I do not know how it is with SLSvcUtil.exe as the proxy generator with Monotouch, but I always used it in combination with Silverlight, as Silverlight is WP7 native, it is why it works there.
In MT you probably need to do it MT way, open the MonoDevelop and add the reference to the service from there so it is created using the Mono framework and its WCF proxy implementation rather than generated code for Silverlight service proxy.
At least, this works for me and works well to WCF services using basic HTTP binding.
It turns out that this was a bug in the Mono framework. As of MonoTouch 4.0.1, this is resolved, so I can use the above code without problems.

accessing WCF service through URL

I have a WCF service ( Let's say WCFService1 ) is deployed on two remote machines. Since the same service is deployed on two different machines they have common interface and common methods exposed.
WCFService1 is deployed on Machine1 and Machine2.
To consume WCF service from client machine, I have created a client app:
I have added a design time reference of WCF service (WCFService1 )( with the help of URL http://11.12.25.23/WCFService/Service1.svc).
Now I can invoke the methods exposed in the service. Up until now its fine...
Now my question is If I have to update client at run time with same service hosted in different machine with different URL ( Let's say http://12.12.24.24/WCFService/Service1.svc), How can I do that?
At present I am doing this:
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://12.12.24.24/WCFService/Service1.svc");
MyServiceClient serviceClient = new MyServiceClient(binding, address);
but whenever I use to invoke the method exposed in the service I got binding mis match error.
Have you tried invoking your client first?
eg:
MyWCFClient client = new MyWCFClient();
client.EndPoint.Address = new EndpointAddress("http://somewhere:888/here.svc");
I'd suspect, that if you look in your web.config file on Machine1, you'll see that the binding there is WSHttpBinding (or something different than BasicHttpBinding). If you change it to BasicHttpBinding (assuming that is what you really want), you'll remove this error.
How is your service configured? Show us your server-side and client-side config!
Binding mismatch means you're either not using the same binding, or some vital parameter on the binding is different - there must be something configured wrong - so show us the config!
Marc

How can I set an HTTP Proxy (WebProxy) on a WCF client-side Service proxy?

How can I set the HTTP proxy programmatically, on a WCF client, without using the default proxy?
Proxies, proxies, proxies.
According to the WCF model of development, I generate client-side "proxy"
classes by running svcutil.exe on the WSDL for the service. (This also
produces a client-side config file).
In my code I new-up an instance of that class and I can connect to the
service. Very nice.
var svcProxy = new MyWebService();
svcProxy.GetInformation(request);
We call this thing a proxy class, but there is another proxy - the http proxy. This
service is using wsHttpBinding basicHttpBinding, so it is going over
http. Now, suppose I want to connect the client to the web service over
a http proxy (modeled by a System.Net.WebProxy in the .NET BCL). I know
from my extensive, delightful experience reading .NET and WCF documentation, that
the WCF runtime, if not instructed otherwise, will use the default
system proxy when communicating over http/https.
I can set that from the command line in
WinXP / 2003 with ProxyCfg.exe as described here, and in later
versions of Windows with netsh.exe as described here.
I can also specify the default web proxy for use within the application
by setting the System.Net.WebRequest.DefaultWebProxy property.
But suppose I want to connect over a proxy that is different than the
system-wide proxy? For instance maybe there is no system-wide proxy but
I need to use one for the web service in particular. Or maybe there is
a system-wide proxy but I need to use a different one, for the web
service. And in fact maybe there are multiple web service clients, and
each one should get a different proxy.
How can the proxy be set per-binding?
In the ASMX model, I could do this:
var svcProxy = new MyWebService();
svcProxy.Proxy = new System.Net.WebProxy("http://proxyserver:1234", true);
svcProxy.GetInformation(request);
But this is not possible with WCF; the WCF-generated client-side proxy
classes do not expose a Proxy property. How do I set the http proxy, per client-side proxy, and how do I set authentication on the http proxy as well?
Related:
- how-to-set-proxy-with-credentials-to-generated-wcf-client
It makes sense that there is no Proxy property on the WCF proxy, because not all WCF proxies use HTTP for communication. After further review, I found that it is possible to set the proxy in WCF programmatically, if the WCF proxy uses an HTTP binding. I am documenting it here in case someone else needs it. To set the HTTP Proxy in code for a WCF client, do this:
// instantiate a proxy for the service
var svc= new ServiceClient();
// get the HTTP binding
var b = svc.Endpoint.Binding as System.ServiceModel.BasicHttpBinding;
b.ProxyAddress = new Uri("http://127.0.0.1:8888");
b.BypassProxyOnLocal = false;
b.UseDefaultWebProxy = false;
And to set the endpoint address - where to reach the server - in code, you would do something like this:
var e = svc.Endpoint;
e.Address = new System.ServiceModel.EndpointAddress(
"http://remoteserver:5555/WcfXmlElement");
The proxy settings are part of the binding configuration. For example, look at the ProxyAddress property of the BasicHTTPBinding and WSHttpBinding classes/configuration elements.
Looks like you're leaving your endpoint configuration in the app.config file, in which case you should be able to set the address there.
I have had a similar problem, but I also needed to use a username and password for the proxy that differ from the username and password used to access the service.
I tried building it up through a UriBuilder, which would output the proxy address as "http://username:password#myproxyserver/". Unfortunately, the particular proxy I was using didn't work with this technique.
What I found after extensive Googling, is that you can change the proxy through WebRequest.DefaultProxy (static property).
For example:
WebProxy proxy = new WebProxy("http://myproxyserver",true);
proxy.Credentials = new NetworkCredential("username", "password");
WebRequest.DefaultWebProxy = proxy;
You could also try this :
Programmatically get whatever binding you are using,and then set the proxy on it directly e.g.
var binding = new WSDualHttpBinding("WSDualHttpBinding_IMainService");
binding.ProxyAddress = new Uri("http://192.168.5.1:3128");
where "WSDualHttpBinding_IMainService" is the name of your binding from your config file.
Also you have to set UseDefaultWebProxy=false; otherwise your proxy will be ignored.
I found a solution for someone who might use .NET Core 2.x
Which initially cause an issue when trying to set proxy by using
System.ServiceModel.BasicHttpBinding;
as answered by Cheeso.
You have to update WCF to the latest (v.4.7.0).
Go to NuGet Package Manager -> Update all related Project URL of WCF.
There must be:
System.ServiceModel.Security
System.ServiceModel.NetTcp
System.ServiceModel.Http