简体   繁体   English

通过DLL从Windows服务调用WCF服务

[英]Call WCF Service from Windows service via DLL

Basically my situation is this: 基本上我的情况是这样的:

Windows Service --Calls--> MethodinDLL --Calls--> WCFService.

Service configuration is written on the DLL side manually, and on windows service side in config - I am insterested if WCF parameters on DLL side and on the Windows service side should match. 服务配置是在DLL端手动编写的,而在config的Windows服务端则是这样-如果DLL端和Windows服务端的WCF参数应该匹配,我会感到很困惑。 More elaboration below. 下面有更多阐述。

I will tell you my setup. 我会告诉你我的设置。 I call WCF Service from DLL. 我从DLL调用WCF服务。 There is code inside DLL which calls WCF service such as this: DLL中有代码可以调用WCF服务,例如:

//Inside DLL
BasicHttpBinding binding = new BasicHttpBinding(SecurityMode.None);
EndpointAddress epa = new EndpointAddress("http://localhost:16593/FreeSpaceDatabase.svc");

YourServiceClient proxy = new YourServiceClient(binding, epa);

I manually provide parameters as you can see. 如您所见,我手动提供了参数。

Now, I have a windows service which actually refers to this DLL, and using DLL (or via DLL), calls the WCF Service. 现在,我有一个Windows服务,该服务实际上引用了此DLL,并使用DLL(或通过DLL)调用了WCF服务。 (The call to WCF Service is located in DLL - windows service doesn't have method to call WCF Service). (对WCF服务的调用位于DLL中-Windows服务没有调用WCF服务的方法)。

But I was forced to still add this on the Windows service side: 但是我仍然被迫将其添加到Windows服务端:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IFreeSpaceDatabase" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:16593/FreeSpaceDatabase.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFreeSpaceDatabase"
                contract="ServiceReference1.IFreeSpaceDatabase" name="BasicHttpBinding_IFreeSpaceDatabase" />
        </client>
    </system.serviceModel>
</configuration>

My question is what if the EndpointAddress("http://localhost:16593/FreeSpaceDatabase.svc") which I specify in the code on the DLL side (like in the beginning of my question): 我的问题是,如果我在DLL端的代码中指定了EndpointAddress("http://localhost:16593/FreeSpaceDatabase.svc") (如我的问题的开头EndpointAddress("http://localhost:16593/FreeSpaceDatabase.svc") ,该怎么办:

  EndpointAddress epa = new EndpointAddress("http://localhost:16593/FreeSpaceDatabase.svc");

is different from the endpoint address specified in the AppConfig file as of the Windows service ? Windows服务AppConfig文件中指定的终结点地址不同吗? Is it allowed? 可以吗 Or they should always be the same? 还是应该总是一样?

You can remove this address from your config and it should work. 您可以从您的配置中删除该地址,它应该可以正常工作。 I have these settings (bindings and endpoints) in my code and only this in my config: 我的代码中有这些设置(绑定和端点),配置中只有这些设置:

<system.serviceModel>
    <bindings />
    <client />
</system.serviceModel>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM