2011-06-17 17 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メソッドがうまく動作しません。 Iveはpluralsightからの例をコピーしました。唯一の違いはサービスホストアプリケーションの代わりに.svcファイルでサービスをホストしようとしていることです...

誰かが正しい方向に私を向けることができますか?

+0

クライアントのapp.configに何か問題がありますか?あなたはその1つを掲示しなかった –

答えて

3

が見える

あなたは http://localhost:52587/Api/Content/VLSContentService.svc/submit

UriTemplateは

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

変更されたエンドポイントのアドレスに相対的であるに掲示しなければならない

このコード行を

WebChannelFactory cs = new WebChan nelFactory(新しいUri( "http:// localhost:52587/Api/Content/VLSContentService.svc /"));

+0

こんにちはリチャード、ありがとう私は今参照してください。 didntはclient.SubmitVideoPOSTへの私の呼び出しがPOSTパケットのどこかに 'submit'コマンドを追加することを理解しています...ありがとう – Exitos

3

あなたは間違ったURLに投稿されているようです。エラーはあなたが相対アドレス "/ SubmitVideoPOST/submit"に投稿したことを示していますが、そのメソッドのUriTemplateは単に "/ submit"です。

RESTベースの要求のURLに.NETメソッド名を含める必要はありません。 UriTemplateだけが重要です。正しいランタイムメソッドへのマッピングは、WCF REST UriTemplate処理エンジンによって実行されます。あなたが間違ったサービスのアドレスを持っているよう

関連する問題