2012-07-16 17 views
8

我是WCF的初學者,我正在學習Essential WCF。WCF InvalidOperationException:綁定實例已經關聯到偵聽URI

使用ServiceContract NameSpace和Name時遇到問題。當我運行代碼時,我捕捉到一個InvalidOperationException異常。但我不明白。

一個綁定實例已經關聯到偵聽URI'http:// localhost:8080/NamespaceChange01'。如果兩個端點想要共享相同的ListenUri,則它們也必須共享相同的綁定對象實例。兩個衝突的端點是在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

兩個端點(basic和mex)不能在同一個地址上。爲其中一個(或兩個)添加一些特定的地址。

例如:

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

在創建服務類時,爲什麼在您的代碼中指定了ServiceContract屬性?

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

請刪除那一個,然後再試一次。

+1

這是不正確-1 - 在OP的代碼是完全有效的。這是不正確的配置 – 2012-07-16 15:17:04

5

你從你的元數據終結缺少地址屬性:

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

沒有它,WCF認爲要主持MEX終結點在同一地址。

+0

客戶端應用程序正在使用mexHttpsBinding和服務器使用wsHttpsBinding,我也得到同樣的錯誤「客戶端和服務綁定不匹配」 – 2014-12-22 14:36:17

相關問題