Async WCF client calls with custom headers: This OperationContextScope is being disposed out of order

According to Microsoft documentation: Do not use the asynchronous “await” pattern within a OperationContextScope block. When the continuation occurs, it may run on a different thread and OperationContextScope is thread specific. If you need to call “await” for an async call, use it outside of the OperationContextScope block. So the simplest proper solution is: Task<ResponseType> … Read more

WCF change endpoint address at runtime

So your endpoint address defined in your first example is incomplete. You must also define endpoint identity as shown in client configuration. In code you can try this: EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity(“host/mikev-ws”); var address = new EndpointAddress(“http://id.web/Services/EchoService.svc”, spn); var client = new EchoServiceClient(address); litResponse.Text = client.SendEcho(“Hello World”); client.Close(); Actual working final version by valamas EndpointIdentity … Read more

Error consuming webservice, content type “application/xop+xml” does not match expected type “text/xml”

For anyone suffering from the same problem; I’ve found a solution for consuming the web service as a Service Reference (WCF). The BasicHttpBinding.MessageEncoding property needs setting to “Mtom”. Here’s a snippet of the required config setting: <configuration> <system.serviceModel> <bindings> <basicHttpBinding> <binding messageEncoding=”Mtom”> </binding> </basicHttpBinding> </bindings> </system.serviceModel> </configuration> Edit: If you are having the same issue … Read more

Is it possible to call Dynamics CRM 2011 late-bound WCF Organization service without the SDK – straight customized binding?

It is probably possible, but hugely complicated. We had a project using Dynamics which moved to ADFS, and required adding lots of extra code around refreshing tokens (code form autorefreshsecuritytoken.cs, deviceidmanager.cs and toolserviceproxies.cs from the SDK) and that was still using the SDK for everything. Bare in mind you also need windows.identification installed in the … Read more

How to programmatically connect a client to a WCF service?

You’ll have to use the ChannelFactory class. Here’s an example: var myBinding = new BasicHttpBinding(); var myEndpoint = new EndpointAddress(“http://localhost/myservice”); using (var myChannelFactory = new ChannelFactory<IMyService>(myBinding, myEndpoint)) { IMyService client = null; try { client = myChannelFactory.CreateChannel(); client.MyServiceOperation(); ((ICommunicationObject)client).Close(); myChannelFactory.Close(); } catch { (client as ICommunicationObject)?.Abort(); } } Related resources: How to: Use the ChannelFactory

What is the best workaround for the WCF client `using` block issue?

Actually, although I blogged (see Luke’s answer), I think this is better than my IDisposable wrapper. Typical code: Service<IOrderService>.Use(orderService=> { orderService.PlaceOrder(request); }); (edit per comments) Since Use returns void, the easiest way to handle return values is via a captured variable: int newOrderId = 0; // need a value for definite assignment Service<IOrderService>.Use(orderService=> { newOrderId … Read more