1 つのソースからデータを取得し、操作し、データベースに保存するなど、長時間実行されるサービスがあります。
そのサービスのいくつかのメソッドを他のアプリケーションに公開したいと思います。現在、.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();
}
次に、ServiceHost クラスを介して WCF サービスを自己ホストする実際の Windows サービスを作成します。
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()
ため、呼び出したときに null 参照を取得することを意味します。_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>