我的情况:
我有一个Windows服务" A"和wcf服务" B"它托管在其他Windows服务中。在我的Windows服务A中,我创建了一个列表,然后我想将其传递给wcf服务B.然后wcf服务B编辑此列表并将其传回。
我应该如何连接这两项服务?我是否必须在wcf服务中引用服务A?或者我是否必须从我的Windows服务A中创建客户端并添加服务引用?
我感谢任何帮助。我花了很多时间在google stackoverflow和msdn上搜索,但无法找到 帮助我的东西。
修改
Windows服务
public long GetSize(string path)
{
DirectoryInfo direc = new DirectoryInfo(path);
long size = CalculateDirectorySize(direc, true);
return size;
}
WCF服务
using WindowsService;
WindowsServiceMethode wsm = new WindowsServiceMethod();
public long GetWcfCheck(string path)
{
long size = wsm.GetSize(path);
return size;
}
ASP.Net Webapp
public ActionResult Index()
{
WCF.CommunicationServiceClient client = new WCF.CommunicationServiceClient("BasicHttpBinding_ICommunicationService");
ViewBag.s = client.GetWcfCheck(@"_somepath_").ToString();
return View("ShowView");
}
这是传递数据的正确方法吗?
答案 0 :(得分:0)
这是我从你的问题中可以理解的:
如果这一切都是正确的,那么你应该做的事情是:
应将WCF服务配置为通过NetNamedPipes进行连接,因为正如该文章所述,
NetNamedPipeBinding提供了一种安全可靠的绑定,可针对机上通信进行优化。
Service2需要向WCF服务添加服务参考,您可以找到如何执行此操作here。
现在Service1知道Service2(并且Service2完全没有必要知道Service1),您必须声明您的服务方法以允许双向通信。这是我做过的一个实现的例子:
[ServiceContract(Namespace = "http://Your.Namespace", SessionMode = SessionMode.Required)]
public interface ICommunicator
{
[OperationContract(IsOneWay = false)]
string StartServer(string serverData);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class CommunicatorService : ICommunicator
{
public string StartServer(string serverData)
{
//example method that takes a string as input and returns another string
return "Hello!!!!";
}
}
编辑:添加客户端和服务器配置。但是,我必须告诉你,由于我遇到了多个不相关的app.config问题,客户端配置是通过代码完成的。
服务器app.config:
<system.serviceModel>
<services>
<service behaviorConfiguration="MyNamespace.CommunicatorServiceBehavior" name="MyNamespace.CommunicatorService">
<endpoint address="" binding="netNamedPipeBinding" name="netNamedPipeCommunicator" contract="MyNamespace.ICommunicator">
</endpoint>
<endpoint address="mex" binding="mexNamedPipeBinding" name="mexNamedPipeCommunicator" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.pipe://localhost/CommunicatorService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyNamespace.CommunicatorServiceBehavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
客户代码:
private static ICommunicator Proxy;
ServiceEndpoint endpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(ICommunicator)))
{
Address = new EndpointAddress("net.pipe://localhost/CommunicatorService"),
Binding = new NetNamedPipeBinding()
};
Proxy = (new DuplexChannelFactory<ICommunicator>(new InstanceContext(this), endpoint)).CreateChannel();
((IClientChannel)Proxy).Open();
public void StartServer(string serverData)
{
string serverStartResult = Proxy.StartServer(serverData); // calls the method I showed above
}