kyr*_*isu 12 c# wcf interprocess net.tcp
我正在尝试在Windows服务中托管的WCF和我的服务GUI之间进行通信.问题是我正在尝试执行OperationContract方法
"'net.tcp:// localhost:7771/MyService'中的ChannelDispatcher与合同'"IContract"'无法打开其IChannelListener."
我的app.conf看起来像这样:
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBinding">
<security>
<transport protectionLevel="EncryptAndSign" />
</security>
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:7772/MyService" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior"
name="MyService.Service">
<endpoint address="net.tcp://localhost:7771/MyService" binding="netTcpBinding"
bindingConfiguration="netTcpBinding" name="netTcp" contract="MyService.IContract" />
</service>
</services>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
端口7771正在侦听(使用netstat检查),svcutil能够为我生成配置.
任何建议,将不胜感激.
从异常堆栈跟踪
Run Code Online (Sandbox Code Playgroud)Server stack trace: at System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter) at System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
有一个内部的exeption(但不在Exeption.InnerExeption下但在Exeption.Detail.InnerExeption下 - ToString()方法没有显示)
已经存在URI'net.tcp:// localhost:7771/MyService'的注册.
但是我的服务只在app.config文件中指定了这个URI.在整个解决方案中,此URI在服务器中一次和客户端一次.
我解决了它:D
以下是问题的解释:
第一个BAD代码:
namespace WCFServer
{
public class Program : IWCFService
{
private ServiceHost host;
static void Main(string[] args)
{
new Program();
}
public Program()
{
host = new ServiceHost(typeof(Program));
host.Open();
Console.WriteLine("Server Started!");
Console.ReadKey();
}
#region IWCFService Members
public int CheckHealth(int id)
{
return (1);
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,服务合同是在托管服务的类中实现的.这导致整个错误的事情(也许typeof()运行一个构造函数,我不知道我对这个问题有建设性的意见).
好的代码:
namespace WCFServer
{
public class Program
{
private ServiceHost host;
static void Main(string[] args)
{
new Program();
}
public Program()
{
host = new ServiceHost(typeof(WCF));
host.Open();
Console.WriteLine("Server Started!");
Console.ReadKey();
}
}
public class WCF : IWCFService
{
#region IWCFService Members
public int CheckHealth(int id)
{
return (1);
}
#endregion
}
}
Run Code Online (Sandbox Code Playgroud)
两个文件的服务合同:
[ServiceContract]
public interface IWCFService
{
[OperationContract]
int CheckHealth(int id);
}
Run Code Online (Sandbox Code Playgroud)
App.config中
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WCFBehavior" />
</serviceBehaviors>
</behaviors>
<bindings>
<netTcpBinding>
<binding name="tcpBinding">
<security>
<transport>
<extendedProtectionPolicy policyEnforcement="Never" />
</transport>
</security>
</binding>
</netTcpBinding>
</bindings>
<services>
<service name="WCFServer.WCF">
<endpoint address="net.tcp://localhost:1111/TcpIHostMonitor" binding="netTcpBinding"
bindingConfiguration="tcpBinding" name="netTcpEndpoint" contract="WCFServer.IWCFService" />
</service>
</services>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
我知道你已经解决了问题,但没有人指出为什么解决了问题,以及导致错误的原因。您的第一个代码的问题是您的程序(类程序)指定了对本身就是“类程序”的类的开放调用,换句话说,它是递归的。难怪它给出了无法打开监听器的错误!这是一件好事!:-)
| 归档时间: |
|
| 查看次数: |
31750 次 |
| 最近记录: |