2012-07-16 15 views
8

私はWCFの初心者であり、Essential WCFで勉強しています。WCF InvalidOperationException:バインディングインスタンスがすでにリスニングURIに関連付けられています

ServiceContract NameSpaceおよびNameを使用するときに問題が発生しました。 コードを実行すると、InvalidOperationExceptionが発生します。しかし、私ははっきり理解できませんでした。

バインディング・インスタンスは、すでにURI「http:// localhost:8080/NamespaceChange01」をlistenするために関連付けられています。 2つのエンドポイントが同じListenUriを共有したい場合、同じバインディングオブジェクトインスタンスも共有する必要があります。競合する2つのエンドポイントは、AddServiceEndpoint()呼び出し、設定ファイル、またはAddServiceEndpoint()とconfigの組み合わせで指定されています。

誰かがInvalidOperationExceptionを利用する方法を知っていますか?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.ServiceModel; 

namespace NamespaceChange01 
{ 

    [ServiceContract(Name = "MyServiceName", Namespace = "http://ServiceNamespace")] 
    public interface IBurgerMaster 
    { 
     [return: MessageParameter(Name = "myOutput")] 
     [OperationContract(Name = "OperationName", Action = "OperationAction", ReplyAction = "ReplyActionName")] 
     double GetStockPrice(string ticker); 
    } 

    [ServiceBehavior(Namespace = "http://MyService")] 
    public class BurgerMaster : IBurgerMaster 
    { 

     public double GetStockPrice(string ticker) 
     { 
      return 100.99; 
     } 
    } 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      ServiceHost host = new ServiceHost(typeof(BurgerMaster)); 
      host.Open(); 
      Console.ReadLine(); 
      host.Close(); 
     } 
    } 
} 
  • app.configを

    <?xml version="1.0" encoding="utf-8" ?> 
    <configuration> 
        <system.serviceModel> 
        <services> 
         <service name="NamespaceChange01.BurgerMaster" behaviorConfiguration="mexServiceBehavior"> 
         <host> 
          <baseAddresses> 
          <add baseAddress="http://localhost:8080/NamespaceChange01"/> 
          </baseAddresses> 
         </host> 
         <endpoint name="basic" binding="basicHttpBinding" contract="NamespaceChange01.IBurgerMaster"/> 
         <endpoint name="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
         </service> 
        </services> 
        <behaviors> 
         <serviceBehaviors> 
         <behavior name="mexServiceBehavior"> 
          <serviceMetadata httpGetEnabled="true"/> 
         </behavior> 
         </serviceBehaviors> 
        </behaviors> 
        </system.serviceModel> 
    </configuration> 
    

感謝。

答えて

16

2つのエンドポイント(基本とmex)が同じアドレスにあることはできませんでした。そのうちの1つ(または両方のアドレス)に特定のアドレスを追加します。例えば

<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
-2

サービスクラスを作成しているときに、なぜコードに記載されているようにServiceContract属性でマークしましたか?

[ServiceBehavior(Namespace = "http://MyService")] 
public class BurgerMaster : IBurgerMaster 

これを削除してもう一度お試しください。

+1

-1これは間違っている - OPのコードが完全に有効です。それは間違っている設定です –

5

あなたのメタデータエンドポイントからアドレス属性逃している:それWCFなければ

<endpoint name="mex" binding="mexHttpBinding" contract="IMetadataExchange" address="mex" /> 

は、あなたが同じアドレスでMEXエンドポイントをホストしたいと考えています。

+0

クライアントアプリケーションがmexHttpsBindingとwsHttpsBindingを使用しているサーバーを使用しています。同じエラーが発生しています "クライアントとサービスのバインディングの不一致" –

関連する問題