我是 WCF 的初学者,我正在学习 Essential WCF。
我在使用 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();
}
}
}
应用程序配置
<?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>
谢谢。