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.
Related
i have and app that use a wcf service reference with the enableHttpCookieContainer="true" in the binding.
enableHttpCookieContainer is not really compatible in visual studio, i need every time that i need to update to delete it.
after few searchs i founded other methods to pass cookies in the wcf requests like this:
using (new System.ServiceModel.OperationContextScope(Channel))
{
System.ServiceModel.Channels.HttpRequestMessageProperty request = new System.ServiceModel.Channels.HttpRequestMessageProperty();
request.Headers["Cookie"] = CoockieContainer;
System.ServiceModel.OperationContext.Current.OutgoingMessageProperties[System.ServiceModel.Channels.HttpRequestMessageProperty.Name] = request;
}
but this don't work for me in wp7.
my question is, what is the officially, clean, compatbile way, to have a session in wcf when use from wp7 service reference? if wcf is hosted in a windows service instead a web site, there is not session, so i really think that the enableHttpCookieContainer is a fail and not clean way...
I have an existing .NET 3.5 based framework that is extended using custom plugins. In summary plugins implement a common interface and the core framework invokes these via reflection. The framework works perfectly and all is good, however...
I now have a requirement that requires a plugin that communicates with the WCF service. At face value this is simple, add a service reference to the plugin, call the client proxy code and off we go. However...
Due to the way that .NET configuration works the WCF service client configuration should reside within the app.config of the executing application. In this case this is my plugin invoker application. The problem with this is that it breaks the plugin "model" as the generic invoker application now has to have plugin specific configuration within it.
So the question is does anybody know of an alternative mechanism for handling the WCF service client configuration without putting it into the core invoker application configuration?
Having done a little hunting around there are mechanisms to allow a DLL to use its own config file. The issue here is that I don't have access to the underlining code of the service proxy creation and therefore seemingly can't redirect the config reads.
A WCF client's endpoint can be configured programmatically as well.
Here's an example that shows how to invoke a WCF service without the need for a configuration file:
var myBinding = new BasicHttpBinding();
var myEndpoint = new EndpointAddress("http://localhost/myservice");
var client = new MyServiceClient(myBinding, myEndpoint);
try
{
client.MyServiceOperation();
client.Close();
}
catch
{
if (client != null)
{
client.Abort();
}
}
Related resources:
ClientBase<TChannel> Constructor (Binding, EndpointAddress)
Answering my own question:
I seem to have found a solution to the problem here:
http://weblogs.asp.net/cibrax/archive/2007/10/19/loading-the-wcf-configuration-from-different-files-on-the-client-side.aspx
In summary this allows you to specify a custom configuration file that contains the WCF configuration as it is generated by Visual Studio - this means that the config can be maintained easily.
Having run a couple of quick tests it seems that it works as it should (with a few tweaks here and there (see the comments on the page).
I am trying to consume a WCF web service from a .NET client application, and I think I need to be able to programmatically create endpoints, but I don't know how. I think I need to do this because, when I try to run the application, I am getting the following error:
Could not find default endpoint
element that references contract
'IEmailService' in the ServiceModel
client configuration section. This
might be because no configuration file
was found for your application, or
because no endpoint element matching
this contract could be found in the
client element.
While troubleshooting this error, I created a simple windows forms application, in which I try to consume the same web service. With this test application I can connect to the web service successfully, and I get a valid response. But, I can reproduce the exact error cited above within in my test app by removing the system.serviceModel node and all of its child nodes from the application's app.config file (I might not have to remove ALL of that section, I'm not sure). So, my first thought was that I need to add that section to the app.config file for the real app, and everything should be fine. Unfortunately, for ridiculous reasons that I won't get into here, that is not an option. So, I am left with having to generate this information in code, inside the client app.
I am hoping someone here can help me work through this, or can point me toward a good resource for this sort of problem.
Is it possible to create endpoint configurations in the client app, in code?
By default, when you do an Add Service Reference operation, the WCF runtime will generate the client-side proxy for you.
The simplest way to use it is to instantiate the client proxy with a constructor that takes no parameters, and just grab the info from the app.config:
YourServiceClient proxy = new YourServiceClient();
This requires the config file to have a <client> entry with your service contract - if not, you'll get the error you have.
But the client side proxy class generated by the WCF runtime also has additional constructors - one takes an endpoint address and a binding, for instance:
BasicHttpBinding binding = new BasicHttpBinding(SecurityMode.None);
EndpointAddress epa = new EndpointAddress("http://localhost:8282/basic");
YourServiceClient proxy = new YourServiceClient(binding, epa);
With this setup, no config file at all is needed - you're defining everything in code. Of course, you can also set just about any other properties of your binding and/or endpoint here in code.
An east way to consume a WCF service if you have a reference to the assembly which defines the interface, is using the System.ServiceModel.ChannelFactory class.
For example, if you would like to use BasicHttpBinding:
var emailService = ChannelFactory<IEmailService>.CreateChannel(new BasicHttpBinding(), new EndpointAddress(new Uri("http://some-uri-here.com/));
If you don't have a reference to the service assembly, then you can use one of the overloaded constructors on the generated proxy class to specify binding settings.
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.
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