Andrew Lubochkn
Andrew Lubochkn

Reputation: 966

Wcf sends xml content type instead of json,

have WCF server and WCF client. Below is the client code:

[GeneratedCode("System.ServiceModel", "3.0.0.0")]
[ServiceContract(ConfigurationName = "IMyService")]
public interface IMyService
{
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, UriTemplate = "DoSmth")]
    [OperationContract(Action = "http://tempuri.org/IConfigService/SetSettings")]
    OrigamiHttpResponse<List<ErrorRecords>> DoSmth(int param);
}

public class MyService: BaseClient<IMyService>
{
    public ConfigServiceClient(AuthResult authObject) : base(authObject, null)
    {
    }

    public OrigamiHttpResponse<List<ErrorRecords>> DoSmth(int param)
    {
        return Proxy.DoSmth(param);
    }
}

public abstract class BaseClient<T> : BaseClient where T : class
{
    protected T Proxy { get; private set; }

    protected BaseClient(AuthResult authObject, IConfig config)
        : base(authObject, config)
    {
        var factory = new ChannelFactory<T>("*");

        if (factory.Endpoint == null || factory.Endpoint.Address == null)
            throw new Exception("WCF Endpoint is not configured");

        if (factory.Endpoint.Address.Uri.Scheme.ToLower() == "https")
            HttpAccess.PrepareSslRequests();

        if (_authObject != null)
        {
            var cb = new CookieBehavior(_authObject);
            factory.Endpoint.Behaviors.Add(cb);
        }

        Proxy = factory.CreateChannel();
    }
}

When I call method DoSmth() from console application, content type is json. But my architecture is that I am calling proxy method and then proxy server acts as client for wcf server and calls wcf method that is my DoSmth(). In this case content type is xml and i can't change it. May be the issue is in operation context, because it is one call from another. Could anyone help please?

Upvotes: 2

Views: 445

Answers (1)

carlosfigueira
carlosfigueira

Reputation: 87293

This is caused by the fact that your WCF client (Proxy) is running in the operation context on the service method (which contains information about the incoming request), and that overrides the context which should be used by the outgoing request. To fix this you need to create a new operation context scope when doing the call, so that it will use the appropriate property from the WebInvoke/WebGet attributes:

public OrigamiHttpResponse<List<ErrorRecords>> DoSmth(int param)
{
    using (new OperationContextScope((IContextChannel)Proxy))
    {
        return Proxy.DoSmth(param);
    }
}

Upvotes: 1

Related Questions