2011-06-17 74 views
3

我有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文件,而不是服務主機應用程序的服務的例子...

可有人點我在正確的方向?

+0

客戶端上的app.config有問題嗎?你沒有發佈那一個 –

回答

3

看起來你有服務錯誤的地址

你應該張貼到http://localhost:52587/Api/Content/VLSContentService.svc/submit

的UriTemplate相對於這

http://localhost:52587/Api/Content/VLSContentService.svc

更改端點的地址這行代碼爲

WebChannelFactory cs = new WebChan nelFactory(new Uri(「http:// localhost:52587/Api/Content/VLSContentService.svc /」));

+0

嗨理查德,謝謝你,我現在看到了。沒有意識到我對client.SubmitVideoPOST的調用會在POST數據包的某處添加'submit'命令...謝謝 – Exitos

3

您似乎發佈到錯誤的URL。該錯誤顯示您發佈到相對地址「/ SubmitVideoPOST/submit」,但您的該方法的UriTemplate只是「/ submit」。

您不需要在基於REST的請求的URL中包含.NET方法名稱。只有UriTemplate很重要。通過WCF REST UriTemplate處理引擎爲您完成映射到正確的運行時方法。

相關問題