gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 14:39:28 28 4
gpt4 key购买 nike

我有 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com