我正在开发一个服务应用程序,它可以允许连接到服务器的物联网设备,通过WCF将字符串数据传输到客户端(我必须在c#中执行此操作)。我能够通过使用带有wsdualhttpbinding的端点编写自托管服务来实现这一点。现在,我想通过webhttpbinding将另一个端点添加到同一服务中,以利用REST功能。可能吗?感谢您是否可以指出我做上面提到的例子。谷歌搜索了几个小时,无法找到一个能够将wsdualhttpbinding和webhttpbinding结合在一起的例子。
答案 0 :(得分:0)
请你可以像这样添加webhttpbinding
<webHttpBinding>
<binding
allowCookies="Boolean"
bypassProxyOnLocal="Boolean"
closeTimeout="TimeSpan"
hostNameComparisonMode="StrongWildCard/Exact/WeakWildcard"
maxBufferPoolSize="integer"
maxBufferSize="integer"
maxReceivedMessageSize="Integer"
name="string"
openTimeout="TimeSpan"
proxyAddress="URI"
receiveTimeout="TimeSpan"
sendTimeout="TimeSpan"
transferMode="Buffered/Streamed/StreamedRequest/StreamedResponse"
useDefaultWebProxy="Boolean">
writeEncoding="UnicodeFffeTextEncoding/Utf16TextEncoding/Utf8TextEncoding"
<security mode="None/Transport/TransportCredentialOnly">
<transport clientCredentialType =
"Basic/Certificate/Digest/None/Ntlm/Windows"
proxyCredentialType="Basic/Digest/None/Ntlm/Windows"
realm="string" />
</security>
<readerQuotas maxArrayLength="Integer" maxBytesPerRead="Integer" maxDepth="Integer" maxNameTableCharCount="Integer" maxStringContentLength="Integer" />
</binding>
</webHttpBinding>
您也可以参考此网站了解更多信息 https://chsakell.com/2013/09/14/wcf-self-hosting-multiple-bindings/
答案 1 :(得分:0)
最后再回答我自己的问题。我设法启用了两个端点wsdualhttpbinding和webhttpbinding并自我托管。
以下代码显示我如何在控制台启用两个端点。
WebServiceHost host1 = new WebServiceHost(typeof(REST), new Uri("http://localhost:8090/"));
host1.AddServiceEndpoint(typeof(IREST), new WebHttpBinding(), "rest");
host1.Description.Endpoints[0].Behaviors.Add(new WebHttpBehavior { HelpEnabled = true });
host1.Open();
ServiceHost host2 = new ServiceHost(typeof(SOAP), new Uri("http://10.121.72.214:8090/soap"));
host2.AddServiceEndpoint(typeof(ISOAP), new WSDualHttpBinding(), "");
ServiceMetadataBehavior smb = host2.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
{
smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host2.Description.Behaviors.Add(smb);
host2.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
}
host2.Open();
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
Console.WriteLine("Host is opened.");
Console.ReadKey();
host1.Close();
host2.Close();
所以当我在浏览器中输入http://localhost:8090/rest和http://localhost:8090/soap时。我能够看到REST和SOAP(WSDualHttpBinding)服务的帮助页面。我也确认它可以在客户端成功回调方法。