How to set Json.Net as the default serializer for WCF REST service

The usage of Extending Encoders and Serializers (see http://msdn.microsoft.com/en-us/library/ms733092.aspx) or other methods of Extending WCF like usage of DataContractSerializerOperationBehavior is very interesting, but for your special problem there are easier solution ways. If you already use Message type to return the results an use WCF4 you can do something like following: public Message UpdateCity(string code, … Read more

How do I pass values to the constructor on my wcf service?

You’ll need to implement a combination of custom ServiceHostFactory, ServiceHost and IInstanceProvider. Given a service with this constructor signature: public MyService(IDependency dep) Here’s an example that can spin up MyService: public class MyServiceHostFactory : ServiceHostFactory { private readonly IDependency dep; public MyServiceHostFactory() { this.dep = new MyClass(); } protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) … Read more

How do I return clean JSON from a WCF Service?

Change the return type of your GetResults to be List<Person>. Eliminate the code that you use to serialize the List to a json string – WCF does this for you automatically. Using your definition for the Person class, this code works for me: public List<Person> GetPlayers() { List<Person> players = new List<Person>(); players.Add(new Person { … Read more

How to add a custom HTTP header to every WCF call?

The advantage to this is that it is applied to every call. Create a class that implements IClientMessageInspector. In the BeforeSendRequest method, add your custom header to the outgoing message. It might look something like this: public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel) { HttpRequestMessageProperty httpRequestMessage; object httpRequestMessageObject; if (request.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject)) { httpRequestMessage = … Read more