嗨,我有一个服务看起来像这样。
[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
的苹果设备
以及将使用NetTcpBinding
的Windows客户端
现在我希望Windows客户端能够使用回调,但是对于Apple设备,我只想忽略回调。
如果我尝试使用WebHttpBinding
托管服务,则会收到此错误
base = {"Contract requires Duplex,
but Binding 'WebHttpBinding' doesn't
support it or isn't configured properly to support it."}
对我来说,这意味着它不起作用,但可以配置为工作?或者他们是否意味着我必须配置和删除我的回调才能正常工作?
那么是否可以忽略WebHttpBinding
的回调而只接收正常的响应呢?
我通过删除有关回调的所有信息来编辑 webhttpbinding 的端点来解决此问题。 所以现在我有两个工作端点,一个带有回调,它们可以共享相同的服务代码。
唯一的缺点是我搞砸了我的 MX,我不知道如何解决这个问题,但我做了一个解决方法,如果您以这种方式编辑设置,则不添加 Webhttpbound。
它并不漂亮,但现在我不必为所有 wcf 方法/操作提供两个单独的服务和重复代码。
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;
}