私は多くの時間を静かに過ごすのに苦労しましたが、それを機能させることができませんでした。何時間も費やした後、メタデータを見ることができますが、操作を正常に呼び出すことができません。以下は手順とコードです。
- WCFサービスライブラリプロジェクトを作成します。
今コード。
サービス契約とDataContract
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace JsonWCFTest
{
[ServiceContract]
public interface IJsonService
{
[OperationContract]
[WebInvoke(Method = "GET",ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,UriTemplate = "data/{id}")]
Product GetProduct(string id);
}
[DataContract(Name = "product")]
public class Product
{
[DataMember(Name = "id")]
public string Id { get; set; }
}
}
サービス。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
namespace JsonWCFTest
{
public class JsonService:IJsonService
{
public Product GetProduct(string id)
{
return new Product {Id = " you have entered " + id};
}
}
}
web.config
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="service.svc" service="JsonWCFTest.JsonService"/>
</serviceActivations>
</serviceHostingEnvironment>
<services>
<service name="JsonWCFTest.JsonService" behaviorConfiguration="jsonTestServiceBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost" />
</baseAddresses>
</host>
<endpoint address="jsonTestEndPoint" behaviorConfiguration="jsonTestEndPointBehavior"
binding="webHttpBinding" contract="JsonWCFTest.IJsonService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="jsonTestServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonTestEndPointBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
このURLを使用しているとき
http://localhost/service.svc/data/1
InternetExplorerでこの以下のエラーが発生します
'/'アプリケーションのサーバーエラー。リソースが見つかりません。説明:HTTP404。探しているリソース(またはその依存関係の1つ)が削除されたか、名前が変更されたか、一時的に使用できなくなった可能性があります。次のURLを確認し、スペルが正しいことを確認してください。
要求されたURL:/service.svc/data/1