我有一個長期運行的服務,從一個源獲取數據,操縱它,把它存儲在數據庫中,等調用WCF引用Windows服務
我想暴露於該服務的一些方法其他應用。目前我們通過.NET Remoting來做到這一點,但我想轉移到WCF。
不幸的是,我連接的端點永遠不是我通過長時間運行的服務暴露的端點。下面是一個簡單的例子:
[ServiceContract]
public interface ITestWcfService
{
[OperationContract]
CounterResult GetCurrentValue();
}
public class TestWcfService : ITestWcfService
{
private ITestWindowsService _service;
public TestWcfService() { /*NOTE: For discoverability*/ }
public TestWcfService(ITestWindowsService service)
{
_service = service;
}
public CounterResult GetCurrentValue()
{
return _service.GetCurrentValue();
}
}
public interface ITestWindowsService
{
CounterResult GetCurrentValue();
}
然後我有我的實際的Windows服務,它通過ServiceHost類自託管WCF服務。
public partial class TestWindowsService : ServiceBase, ITestWindowsService
{
private static ServiceHost _wcfService;
public TestWindowsService()
{
InitializeComponent();
}
public void OnStart(string[] args)
{
//Create instance of WCF, passing in reference to this service instance
TestWcfService wcf = new TestWcfService(this);
_wcfService = new ServiceHost(wcf);
}
public CounterResult GetCurrentValue()
{
//Note: Some logic here
}
}
現在,只是每次去到TestWcfServiceClient()
呼叫時,它使用默認的構造函數,並創建WCF服務的新實例,並且不使用由創建的實例這更多或更少的作品windows服務。這意味着當我撥打GetCurrentValue()
時,我收到空引用,因爲_service
成員尚未設置。
我環顧四周尋找解決方案,並找到一些引用ServiceHostFactory
和ServiceHost
和IInstanceProvider
,但每個似乎都非常非常複雜。
任何你可以提供的想法將不勝感激。
編輯:這是我的ServiceModel信息
<system.serviceModel>
<services>
<service name="WcfService.TestWcfService">
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfService/TestWcfService/" />
</baseAddresses>
</host>
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="basicHttpBinding" contract="WcfService.ITestWcfService">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
是的,我想我要解決的問題是:我如何讓其他應用程序在我的耐用服務上調用方法?現在我們使用一個超級複雜的自定義.NET Remoting庫。我想使用WCF,因爲過去對我們來說非常好。但是,這似乎並不是它通常解決的問題(至少不是簡單的方式) – Killnine
如果實現所需的類來擴展WCF並傳遞任何服務需要或使用「靜態對象」方法我描述你應該能夠做到。換句話說,忘記你想要做什麼。相反,相反,忘記存在自動託管的解決方案,並使用Windows服務自行託管您的WCF。完成後,您只需將缺失的行爲添加到Windows服務。它非常可行。 – tucaz