2

WCFを使用してRESTfulWebサービスを開発しています。すべてのサービスが実装するServiceContractインターフェースが欲しいのですが、各サービスが独自のメソッドを追加で実装することも望んでいます。

Global.asaxファイルで、サービスルートを初期化します。

RouteTable.Routes.Add(new ServiceRoute("iOSAppService", new WebServiceHostFactory(), typeof(Service.iOSAppService)));
RouteTable.Routes.Add(new ServiceRoute("AndroidAppService", new WebServiceHostFactory(), typeof(Service.AndroidAppService)));
RouteTable.Routes.Add(new ServiceRoute("WindowsPhoneAppService", new WebServiceHostFactory(), typeof(Service.WindowsPhoneAppService)));

IAppService各サービスは、インターフェースを実装する必要があります。

[ServiceContract]
public interface IAppService

これは次のように実装されます。

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class iOSAppService : IAppService

ただし、たとえばiOSAppService、IiOSApServiceインターフェイスを実装することもできます。

[ServiceContract]
public interface IiOSAppService

したがって、実装は次のようになります。

public class iOSAppService : IAppService, IiOSAppService

ただし、これにより次の例外が発生します。

サービス「iOSAppService」は複数のServiceContractタイプを実装し、構成ファイルにエンドポイントは定義されていません。WebServiceHostはデフォルトのエンドポイントを設定できますが、サービスが単一のServiceContractのみを実装している場合に限ります。単一のServiceContractのみを実装するようにサービスを変更するか、構成ファイルでサービスのエンドポイントを明示的に定義します。

誰かが私の目標を達成する方法を知っていますか?

4

1 に答える 1

3

次のような特定のインターフェイスを作成します。

[ServiceContract]
public interface IiOSAppService : IAppService

その後

public class iOSAppService : IiOSAppService

編集:

サービス側では、次のものがあることを確認してください。

<system.serviceModel>
  <services>
    <service name="YourNamespace.iOSAppService">
      <endpoint binding="webHttpBinding" contract="YourNamespace.IiOSAppService" behaviorConfiguration="web">
      </endpoint>
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="web">
        <webHttp />
      </behavior>
    </endpointBehaviors>
  </behaviors>
</system.serviceModel>
于 2012-07-25T12:35:10.307 に答える