我已经通过tcp实现了客户端 - 服务器应用程序。我使用的是异步模式,因为我只限于.NET 3.5。在这种情况下,我被迫使用ChannelFactory以便在客户端和服务器之间进行通信,而无需生成代码。这个简单的应用程序基于一个简单的MSDN wcf异步示例(尽管没有客户端代码)。服务托管在Windows服务中。当我调用Begin方法时,一切正常,但是当我尝试从回调中的End方法获得结果时,我得到TCP错误10061:“无法建立连接,因为目标机器主动拒绝它......”。合同保存在单独的DLL中,该DLL包含在服务实现DLL和客户端DLL中。服务和Windows服务的配置文件是相同的。
这是我的合同
[ServiceContractAttribute]
public interface ISampleService
{
[OperationContractAttribute]
string SampleMethod(string msg);
[OperationContractAttribute(AsyncPattern = true)]
IAsyncResult BeginSampleMethod(string msg, AsyncCallback callback, object asyncState);
string EndSampleMethod(IAsyncResult result);
}
IAsyncResult实施:
public class CompletedAsyncResult<T> : IAsyncResult
{
T data;
public CompletedAsyncResult(T data)
{ this.data = data; }
public T Data
{ get { return data; } }
#region IAsyncResult Members
public object AsyncState
{ get { return (object)data; } }
public WaitHandle AsyncWaitHandle
{ get { throw new Exception("The method or operation is not implemented."); } }
public bool CompletedSynchronously
{ get { return true; } }
public bool IsCompleted
{ get { return true; } }
#endregion
}
服务实施:
public class SampleService : ISampleService
{
public string SampleMethod(string msg)
{
return "sync " + msg;
}
// This asynchronously implemented operation is never called because
// there is a synchronous version of the same method.
public IAsyncResult BeginSampleMethod(string msg, AsyncCallback callback, object asyncState)
{
Console.WriteLine("BeginSampleMethod called with: " + msg);
return new CompletedAsyncResult<string>(msg);
}
public string EndSampleMethod(IAsyncResult r)
{
Thread.Sleep(5000);
CompletedAsyncResult<string> result = r as CompletedAsyncResult<string>;
Console.WriteLine("EndSampleMethod called with: " + result.Data);
return result.Data + " after processing";
}
}
服务配置:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="TcpUnsecured">
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport" />
</binding>
</netTcpBinding>
</bindings>
<services>
<service behaviorConfiguration="Cube.Service1Behavior" name="Cube.ISampleService">
<clear />
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
name="mex" contract="IMetadataExchange" listenUriMode="Explicit">
<identity>
<dns value="localhost" />
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<endpoint address="net.tcp://DavePC:1430/Test"
binding="netTcpBinding"
bindingConfiguration="TcpUnsecured"
name="tcp"
contract="ns.ISampleService" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://DavePC:1430/Test" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Cube.Service1Behavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户端实施:
public delegate void AsyncSampleAction(string result, ISampleService c);
class Program
{
static void Main(string[] args)
{
TestMSDNAsyncWCF();
}
private static void TestMSDNAsyncWCF()
{
ISampleService channel = GetChannelSampleService();
IAsyncResult result = channel.BeginSampleMethod("FINALLY ASYNC", FinishSampleAction, channel);
Console.WriteLine("Press any key to continue . . .");
Console.ReadKey(false);
}
static void FinishSampleAction(IAsyncResult ar)
{
var state = (ISampleService) ar.AsyncState;
string msg = state.EndSampleMethod(ar);
Console.WriteLine(msg);
}
private static ISampleService GetChannelSampleService()
{
ChannelFactory<ISampleService> factory = null;
factory = new ChannelFactory<ISampleService>("tcp");
ISampleService channel = factory.CreateChannel();
return channel;
}
}
客户端配置:
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="TcpUnsecured">
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="false" />
<security mode="Transport" />
</binding>
</netTcpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="Cube.Service1Behavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<client>
<endpoint name="tcp"
address="net.tcp://DavePC:1430/Test"
contract="ns.ISampleService"
binding="netTcpBinding"
bindingConfiguration="TcpUnsecured" />
</client>
</system.serviceModel>
</configuration>
我知道这很冗长但我现在看不到解决方案。