c# - 为什么我的 POST 方法不起作用?

标签 c# wcf

我有 WCF 服务契约(Contract):

[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);
}

我有实现契约(Contract)的服务:

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");

}

但是我收到了这个错误,我不知道为什么:

There was no endpoint listening at http://localhost:52587/Api/Content/VLSContentService.svc/SubmitVideoPOST/submit that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

我知道当我浏览到 URL 中的 GET 方法并传递正确的参数时,我得到了 xml,但我的 POST 方法不起作用。我从 pluralsight 复制了这个例子,唯一的区别是嗯试图在 .svc 文件而不是服务主机应用程序中托管服务......

谁能指出我正确的方向?

最佳答案

看起来你的服务地址有误

您应该发布到 http://localhost:52587/Api/Content/VLSContentService.svc/submit

UriTemplate 是相对于端点地址的

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

将这行代码改为

WebChannelFactory cs = new WebChannelFactory(new Uri("http://localhost:52587/Api/Content/VLSContentService.svc/"));

关于c# - 为什么我的 POST 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6387759/

相关文章:

c# - 为什么编译器会发现这有歧义?

c# - 我可以使用 C# 编写带 Socket 的蓝牙 PC 客户端吗?

大型/复杂模式的 WCF 代码生成 (HR-XML/OAGIS) - 是否有替代方案?

wcf - 如何以soap格式WCF设置属性

c# - WCF ChannelFactory.CreateChannel() 方法

.net - 如何通过 Internet 分发 WCF 点对点应用程序?

c# - 如何在ListView中获取水平滚动条的高度

c# - 是否可以在 App.config 文件中的 appSettings 中引用主目录?

c# - 是否可以将 MongoDB 的 "ISODate"字段反序列化为 JToken (C#)?

wcf - 在 IIS 中托管 WCF net.pipe 绑定(bind)时控制命名管道的名称