複数のコントラクトを実装する WCF Rest Service を開発しています (現在 2 つ)。デフォルトの Web サイトの下にある 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>
何か不足していますか?