WCF 忽略 webhttpbinding 中的双工

WCF ignore duplex in webhttpbinding

嗨,我有一个服务看起来像这样。

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple, UseSynchronizationContext = false)]
public class Service : IService
{
    public string test(string value)
    {
        IServiceCallBack callback = OperationContext.Current.GetCallbackChannel<IServiceCallBack>();

        callback.callBackTest("callBack response");
        return value + ", normal response";
    }
}

[ServiceContract(CallbackContract = typeof(IServiceCallBack))]
public interface IService
{
    [OperationContract]
    [WebInvoke(
        ResponseFormat = WebMessageFormat.Json,
        RequestFormat = WebMessageFormat.Json,
        Method = "POST")]
    string test(string value);
}

public interface IServiceCallBack
{
    [OperationContract]
    void callBackTest(string value);
}

现在我的需求是两个不同的绑定将使用相同的服务(如果可能的话)我知道我可以制作两个完整的独立服务为此工作,但我宁愿不这样做因为两个绑定将使用相同的功能

我的情况是我们有一些客户端是苹果设备,它们将使用 WebHttpBinding

和 windows 个将使用 NetTcpBinding

的客户

现在我希望 windows 客户端能够使用回调,但是对于苹果设备我只想忽略回调。

如果我尝试使用 WebHttpBinding 托管服务,我会收到此错误

base = {"Contract requires Duplex, 
but Binding 'WebHttpBinding' doesn't 
support it or isn't configured properly to support it."}

对我来说,这意味着它无法工作,但可以配置为工作吗? 或者他们的意思是我必须配置和删除我的回调才能工作?

那么是否可以忽略 WebHttpBinding 的回调并只接收正常响应?

我通过删除有关回调的所有信息来编辑 webhttpbinding 的端点来解决这个问题。所以现在我有两个工作端点,一个带有回调,它们可以共享相同的服务代码。

唯一的缺点是我搞砸了我的 mex,我不知道如何解决这个问题,但如果您以这种方式编辑设置,我通过不添加 webhttpbinding 来解决这个问题。

它不是很漂亮,但现在我不必为我的所有 wcf 使用两个单独的服务和重复代码 methods/operations。

public class WebServiceHost : IDisposable
{
    public WebServiceHost()
    {
        _tcpBaseAddress = "net.tcp://localhost:" + Globals.ClientTcpPort + "/V1";
        _httpBaseAddress = "http://localhost:" + Globals.ClientHttpPort + "/V1";

        List<Uri> addresses = new List<Uri>() { new Uri(_httpBaseAddress), new Uri(_tcpBaseAddress)};

        _host = new System.ServiceModel.Web.WebServiceHost(typeof(Service), addresses.ToArray());
        IsOpen = false;
    }

    readonly System.ServiceModel.Web.WebServiceHost _host;
    readonly string _httpBaseAddress;
    readonly string _tcpBaseAddress;

    public static bool IsOpen { get; set; }

    public void OpenHost()
    {
        try
        {
            //WebHttpBinding
            WebHttpBinding webBinding = new WebHttpBinding();
            webBinding.MaxBufferPoolSize = int.MaxValue;
            webBinding.MaxReceivedMessageSize = int.MaxValue;
            webBinding.Security.Mode = WebHttpSecurityMode.None;
            webBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;

            //NetTcpBinding
            NetTcpBinding tcpBinding = new NetTcpBinding();
            tcpBinding.MaxBufferPoolSize = int.MaxValue;
            tcpBinding.MaxReceivedMessageSize = int.MaxValue;
            tcpBinding.Security.Mode = SecurityMode.None;
            tcpBinding.Security.Message.ClientCredentialType = MessageCredentialType.None;

            //ServiceBehavior
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpsGetEnabled = false;
            smb.HttpGetEnabled = true;

            _host.Description.Behaviors.Add(smb);

            ServiceDebugBehavior sdb = _host.Description.Behaviors.Find<ServiceDebugBehavior>();
            sdb.HttpHelpPageEnabled = Globals.IsDebugMode;
            sdb.HttpsHelpPageEnabled = Globals.IsDebugMode;
            sdb.IncludeExceptionDetailInFaults = Globals.IsDebugMode;

            UseRequestHeadersForMetadataAddressBehavior urhfmab = new UseRequestHeadersForMetadataAddressBehavior();
            _host.Description.Behaviors.Add(urhfmab);

            //MEX endpoint
            _host.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexTcpBinding(), "mex");

            if (Globals.UseClientTcpHost && Globals.UseClientHttpHost)
            {
                _host.AddServiceEndpoint(typeof(IService), tcpBinding, _tcpBaseAddress);
                _host.AddServiceEndpoint(typeof(IService), webBinding, _httpBaseAddress);
                _host.Description.Endpoints[2].Contract = CopyContract(_host.Description.Endpoints[1].Contract);
            } 
            else if (Globals.UseClientTcpHost)
            {
                _host.AddServiceEndpoint(typeof(IService), tcpBinding, _tcpBaseAddress);
            }
            else if (Globals.UseClientHttpHost)
            {
                _host.AddServiceEndpoint(typeof(IService), webBinding, _httpBaseAddress);
                _host.Description.Endpoints[1].Contract = CopyContract(_host.Description.Endpoints[1].Contract);
            }

            _host.Open();
            IsOpen = true;

}

复制合约方法

private ContractDescription CopyContract(ContractDescription contract)
    {
        //ContractDescription orgContract = _host.Description.Endpoints[1].Contract;
        ContractDescription value = new ContractDescription("IServiceWithCallBack");

        //copy the value from orgiginal to the new contract.
        foreach (var item in contract.Behaviors)
        {
            value.Behaviors.Add(item);
        }

        value.ConfigurationName = contract.ConfigurationName;
        value.ContractType = contract.ContractType;

        foreach (var item in contract.Operations)
        {
            OperationDescription operation = new OperationDescription(item.Name, value);
            operation.BeginMethod = item.BeginMethod;

            foreach (var behavior in item.Behaviors)
            {
                operation.Behaviors.Add(behavior);
            }

            operation.EndMethod = item.EndMethod;

            foreach (var fault in item.Faults)
            {
                operation.Faults.Add(fault);
            }

            operation.IsInitiating = item.IsInitiating;
            operation.IsTerminating = item.IsTerminating;

            foreach (var knownType in item.KnownTypes)
            {
                operation.KnownTypes.Add(knownType);
            }

            foreach (var message in item.Messages)
            {
                operation.Messages.Add(message);
            }

            operation.ProtectionLevel = item.ProtectionLevel;
            operation.SyncMethod = item.SyncMethod;

            value.Operations.Add(operation);
        }

        value.ProtectionLevel = contract.ProtectionLevel;
        value.SessionMode = contract.SessionMode;

        List<OperationDescription> removeList = new List<OperationDescription>();

        foreach (var item in value.Operations)
        {
            if (item.Name.ToLower().EndsWith("callback"))
                removeList.Add(item);
        }

        foreach (var item in removeList)
        {
            value.Operations.Remove(item);
        }

        return value;
    }