我有WCF服務合同:爲什麼我的POST方法不起作用?
[ServiceContract]
public interface IVLSContentService
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Xml, UriTemplate = "GetCategoriesGET/{userIdArg}", BodyStyle = WebMessageBodyStyle.Bare)]
List<Video> GetVideosGET(string userIdArg);
[WebInvoke(Method = "POST",BodyStyle=WebMessageBodyStyle.Wrapped, UriTemplate = "submit")]
[OperationContract]
void SubmitVideoPOST(Video videoArg, string userId);
}
而且我有一個實現合同服務:
public class VLSContentService : IVLSContentService
{
List<Video> catsForUser1 = new List<Video>();
List<Video> catsForUser2 = new List<Video>();
public List<Video> GetVideosGET(string userIdArg)
{
List<Video> catsToReturn = new List<Video>();
if (Int32.Parse(userIdArg) == 1)
{
catsToReturn = catsForUser1;
}
else if (Int32.Parse(userIdArg) == 2)
{
catsToReturn = catsForUser2;
}
return catsToReturn;
}
public void SubmitVideoPOST(Video videoArg, string userId)
{
int i = 0;
}
}
而且我的配置:
<system.serviceModel>
<services>
<service behaviorConfiguration="VLSContentServiceBehaviour" name="VLSContentService">
<endpoint address="" behaviorConfiguration="VLSContentServiceEndpointBehaviour" binding="webHttpBinding" contract="IVLSContentService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="VLSContentServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="VLSContentServiceEndpointBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
,我試圖使用以下客戶端代碼調用POST WCF操作:
static void Main(string[] args)
{
WebChannelFactory<IVLSContentService> cs = new WebChannelFactory<IVLSContentService>(new Uri("http://localhost:52587/Api/Content/VLSContentService.svc/SubmitVideoPOST"));
IVLSContentService client = cs.CreateChannel();
Video videoToAdd = new Video("My First Video");
client.SubmitVideoPOST(videoToAdd,"1");
}
但即時得到這個錯誤,我不能明白爲什麼:
有沒有終點在 http://localhost:52587/Api/Content/VLSContentService.svc/SubmitVideoPOST/submit 是可以接受的消息聽。這通常是由不正確的地址 或SOAP操作導致的 。有關更多詳細信息,請參見InnerException,如果存在 。
我知道當我瀏覽到一個URL中的GET方法,我傳遞正確的參數我得到的XML返回,但我的POST方法不工作。伊夫複製從pluralsight唯一的區別是UM試圖主辦.svc文件,而不是服務主機應用程序的服務的例子...
可有人點我在正確的方向?
客戶端上的app.config有問題嗎?你沒有發佈那一個 –