【发布时间】:2020-05-14 11:33:21
【问题描述】:
我有一个 WinForms 客户端应用程序。作为我编写的 Windows 服务的一部分,我在另一台计算机上运行 WCF 服务器。我首先尝试了NetPipeBinding,现在尝试了NetTcpBinding。我确实在 具有高级安全性的 Windows 防火墙 中打开了 2 个端口。服务器操作系统是 Windows Server 2016。
这是服务器代码:
public Boolean CreatePipeServer(out string logEntry)
{
try
{
NetTcpBinding ipcBinding = new NetTcpBinding()
{
Security = new NetTcpSecurity()
{
Mode = SecurityMode.None,
Transport = new TcpTransportSecurity()
{
ClientCredentialType = TcpClientCredentialType.None,
ProtectionLevel = System.Net.Security.ProtectionLevel.None,
},
},
MaxBufferPoolSize = 6553600,
MaxBufferSize = 6553600,
MaxReceivedMessageSize = 6553600,
MaxConnections = 1000,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxArrayLength = 2147483647,
MaxBytesPerRead = 2147483647,
MaxDepth = 2147483647,
MaxNameTableCharCount = 2147483647,
MaxStringContentLength = 2147483647,
},
};
// Instantiate: Host Service
Uri uriMex = new Uri("http://localhost:8000");
this.ServiceParent.ServiceIpcAppToService.HostIpcAppToService = new ServiceHost(typeof(IpcAppToService), uriMex);
// Instantiate: Endpoint
Uri uriService = new Uri("net.tcp://localhost:8004/StampIpcAppToService");
this.ServiceParent.ServiceIpcAppToService.HostIpcAppToService.AddServiceEndpoint(typeof(StampIpc.IIpcAppToService), ipcBinding, uriService);
// Instantiate: Service Meta Behavior
ServiceMetadataBehavior ipcSmb = this.ServiceParent.ServiceIpcAppToService.HostIpcAppToService.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (null == ipcSmb)
{
ipcSmb = new ServiceMetadataBehavior()
{
HttpGetEnabled = true,
};
this.ServiceParent.ServiceIpcAppToService.HostIpcAppToService.Description.Behaviors.Add(ipcSmb);
ServiceDescription serviceDescription = this.ServiceParent.ServiceIpcAppToService.HostIpcAppToService.Description;
}
// Instantiate: MEX Biding
Binding ipcMexBinding = MetadataExchangeBindings.CreateMexHttpBinding();
this.ServiceParent.ServiceIpcAppToService.HostIpcAppToService.AddServiceEndpoint(typeof(IMetadataExchange), ipcMexBinding, "MEX");
// Capture events.
...
// Start the service.
this.ServiceParent.ServiceIpcAppToService.HostIpcAppToService.Open();
return true;
}
catch (Exception) { }
return false;
}
我正在尝试通过转到Visual Studio 2019 中的Add > Service Reference... 来测试有效的通信链接。无论我输入什么,我都会不断收到一种或另一种错误。目前,我放置:
net.tcp://10.10.70.134:8004/StampIpcAppToService
这会引发错误:
URI 前缀无法识别。 元数据包含无法解析的引用:“net.tcp://10.10.70.134:8004/StampIpcAppToService”。 元数据包含无法解析的引用:“net.tcp://10.10.70.134:8004/StampIpcAppToService”。 如果在当前解决方案中定义了服务,请尝试构建解决方案并再次添加服务引用。
我已经有了客户端代码,但是如果我不能让Add Service Reference工作,那么客户端代码肯定不会工作。
上面的代码作为Windows Service 的OnStart 事件的一部分开始。
我做错了什么?
更新
答案解决了上面的直接问题,但是我的真实代码使用类似于服务器端的代码,我收到一个错误。我根据答案和一些调试修改了代码。当前代码是:
public static Boolean ConnectToService()
{
ComAppToService.IsPipeAppToService = false;
ComAppToService.IpcPipeFactory = null;
Program.HostIpcAppToService = null;
try
{
// Instantiate: Callack
var ipcCallback = new IpcAppToServiceBack();
InstanceContext ipcCallbackInstance = new InstanceContext(ipcCallback);
// Instantiate: Endpoint
Uri ipcUri = new Uri($"http://10.10.70.134:8000/mex");
EndpointAddress ipcEndpoint = new EndpointAddress(ipcUri);
// Instantiate: Binding
// https://stackoverflow.com/questions/464707/maximum-array-length-quota
WSDualHttpBinding ipcBindingHttp = new WSDualHttpBinding()
{
Security = new WSDualHttpSecurity() { Mode = WSDualHttpSecurityMode.None },
MaxBufferPoolSize = 6553600,
MaxReceivedMessageSize = 6553600,
ClientBaseAddress = ipcUri,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxArrayLength = 2147483647,
MaxBytesPerRead = 2147483647,
MaxDepth = 2147483647,
MaxNameTableCharCount = 2147483647,
MaxStringContentLength = 2147483647,
},
};
// Instantiate: Factory
ComAppToService.IpcPipeFactory = new DuplexChannelFactory<IIpcAppToService>(ipcCallbackInstance, ipcBindingHttp, ipcEndpoint);
Program.HostIpcAppToService = ComAppToService.IpcPipeFactory.CreateChannel();
// Open: Callback
Program.HostIpcAppToService.OpenCallback(IpcCallbackDest.App);
ComAppToService.IsPipeAppToService = true;
return true;
}
catch (Exception ex)
{
// Log the exception.
Debug.WriteLine(ex.Message);
}
return false;
}
例外:
InnerException = {"带有动作的消息 'http://schemas.xmlsoap.org/ws/2005/02/rm/CreateSequence' 不能是 由于 ContractFilter 不匹配,在接收方处理 端点调度程序。这可能是因为合同不匹配 (不匹配的操作...
更新 2(反映更新的答案)
使用NetTcpBinding 会产生此错误:
使用 URI:
Uri ipcUri = new Uri($"net.tcp://10.10.70.134:8004");
生成
Message = "There was no endpoint listening at net.tcp://jmr-engineering:8004/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details."
使用 URI:
Uri ipcUri = new Uri($"net.tcp://10.10.70.134:8000");
生成
Message = "You have tried to create a channel to a service that does not support .Net Framing. It is possible that you are encountering an HTTP endpoint."
InnerException = {"Expected record type 'PreambleAck', found '72'."}
答案:
接受的答案需要修改。亚伯拉罕没有正确的URI,但他将我指向SvcUtil.exe,它给了我。
URI:
net.tcp://10.10.70.134:8004/StampIpcAppToService
我确实需要NetTcpBinding,所以最终的客户端代码是:
/// <summary>
/// Connects the ipc application to service.
/// </summary>
/// <returns>Boolean.</returns>
public static Boolean ConnectToService()
{
ComAppToService.IsPipeAppToService = false;
ComAppToService.IpcPipeFactory = null;
Program.HostIpcAppToService = null;
try
{
// Instantiate: Callack
var ipcCallback = new IpcAppToServiceBack();
InstanceContext ipcCallbackInstance = new InstanceContext(ipcCallback);
// Instantiate: Endpoint
Uri ipcUri = new Uri($"net.tcp://10.10.70.134:8004/StampIpcAppToService");
EndpointAddress ipcEndpoint = new EndpointAddress(ipcUri);
// Instantiate: Binding
// https://stackoverflow.com/questions/464707/maximum-array-length-quota
NetTcpBinding ipcBindingHttp = new NetTcpBinding()
{
Security = new NetTcpSecurity() { Mode = SecurityMode.None },
MaxBufferPoolSize = 6553600,
MaxReceivedMessageSize = 6553600,
MaxBufferSize = 6553600,
MaxConnections = 1000,
ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas()
{
MaxArrayLength = 2147483647,
MaxBytesPerRead = 2147483647,
MaxDepth = 2147483647,
MaxNameTableCharCount = 2147483647,
MaxStringContentLength = 2147483647,
},
};
// Instantiate: Factory
ComAppToService.IpcPipeFactory = new DuplexChannelFactory<IIpcAppToService>(ipcCallbackInstance, ipcBindingHttp, ipcEndpoint);
Program.HostIpcAppToService = ComAppToService.IpcPipeFactory.CreateChannel();
// Open: Callback
Program.HostIpcAppToService.OpenCallback(IpcCallbackDest.App);
ComAppToService.IsPipeAppToService = true;
return true;
}
catch (Exception ex)
{
// Log the exception.
Debug.WriteLine(ex.Message);
}
return false;
}
这是来自SvcUtil.exe 的output.config 的输出。 IpcAppToService.cs 文件没用,但配置文件绝对有用。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_IIpcAppToService">
<security mode="None" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8004/StampIpcAppToService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IIpcAppToService"
contract="IIpcAppToService" name="NetTcpBinding_IIpcAppToService" />
</client>
</system.serviceModel>
</configuration>
【问题讨论】: