我正在开发一个WCF Rest服务,它将实现多个合同(目前为2)。我在默认网站下将解决方案部署到IIS。访问uri:
http://localhost/wcfrestsample/myservices/service1/customers:
我得到以下无效操作异常:
服务'MyService'实现多个ServiceContract类型,并且配置文件中没有定义端点。 WebServiceHost可以设置默认端点,但前提是该服务仅实现单个ServiceContract。将服务更改为仅实现单个ServiceContract,或者在配置文件中显式定义服务的端点。
以下是代码详情:
namespace WcfRestServiceSample
{
//Data Items...
public class Customer
{
public String Address { get; set; }
public String Name { get; set; }
}
public class Employee
{
public String EmployeeNumber { get; set; }
public DateTime JoiningDate { get; set; }
public Double Salary { get; set; }
}
//Service Contracts...
[ServiceContract]
public interface IMyService1
{
[WebGet(UriTemplate = "customers")]
IEnumerable<Customer> GetCustomers();
}
[ServiceContract]
public interface IMyService2
{
[WebGet(UriTemplate = "employees")]
IEnumerable<Employee> GetEmployees();
}
//Service Implementation...
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService1, IMyService2
{
public IEnumerable<Customer> GetCustomers()
{
return new List<Customer>
{
new Customer{ Address = "customer 1 address", Name = "Customer 1" },
new Customer{ Address = "customer 2 address", Name = "Customer 2" }
};
}
public IEnumerable<Employee> GetEmployees()
{
return new List<Employee>
{
new Employee{ EmployeeNumber = "Employee 1", JoiningDate = new DateTime(1995, 5, 5), Salary = 5555.55 },
new Employee{ EmployeeNumber = "Employee 2", JoiningDate = new DateTime(1998, 8, 8), Salary = 8888.88 }
};
}
}
//The Global.asax
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("MyServices", new WebServiceHostFactory(), typeof(MyService)));
}
}
}
配置文件的内容如下:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="MyService">
<endpoint address="service1" binding="webHttpBinding" contract="WcfRestServiceSample.IMyService1" />
<endpoint address="service2" binding="webHttpBinding" contract="WcfRestServiceSample.IMyService2" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<add
name="UrlRoutingModule"
type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</modules>
</system.webServer>
我错过了什么吗?
答案 0 :(得分:0)
使用多个合同时,根本不能使用基地址。即使端点的相对地址将为每个端点创建唯一的URI。这样的愚蠢。 尝试在端点中提及整个地址。我认为它会起作用。
即。使您的终端像:
<endpoint address="http://localhost/wcfrestsample/myservices/service1" binding="webHttpBinding" contract="WcfRestServiceSample.IMyService1" />
<endpoint address="http://localhost/wcfrestsample/myservices/service2" binding="webHttpBinding" contract="WcfRestServiceSample.IMyService2" />