【问题标题】:How to pass parameter and get data from wcf service如何从 wcf 服务传递参数和获取数据
【发布时间】:2019-06-10 05:33:35
【问题描述】:

我收到了这个错误:

http://vkalra.in/WCF_SERVICE/RestServiceImpl.svc 上没有可以接受消息的端点监听。这通常是由不正确的地址或 SOAP 操作引起的。有关详细信息,请参阅 InnerException(如果存在)
InnerException - 远程服务器返回错误:(404) Not Found。

这是我的代码:

Binding binding = new BasicHttpBinding();

// Create endpointAddress of the Service
EndpointAddress endpointAddress = new  EndpointAddress("http://vkalra.in/WCF_SERVICE/RestServiceImpl.svc");

ServiceReference1.Leave_Details emp = new ServiceReference1.Leave_Details();
emp.empid = items.empid;
emp.fromdate = "01-04-2019";
emp.todate = "04-06-2019";
emp.tabt = "1";
emp.jdis = "0";

try
{
    ServiceReference1.RestServiceImplClient service = new ServiceReference1.RestServiceImplClient(binding, endpointAddress);
    string levbal = service.Leave_Calculation(emp);
}
catch(Exception ex)
{
}

【问题讨论】:

  • 您需要在此处提供更多背景信息和信息!!突出的一件事:您正在使用 BasicHttpBinding 这是基本的 SOAP 绑定,但您尝试联系的 URL 似乎是 REST - 这两个不匹配,您需要使用WebHttpBinding 与 REST 端点通信.....
  • 请提供一些示例,以便在服务 Leave_Calculation 中从我们的控制器中的 rest 服务获取数据是采用 5 个参数并返回字符串数据的方法,我是 WCF 服务中的新手?

标签: wcf


【解决方案1】:

如果我们想通过添加服务引用来消费WebHttpBinding创建的WCF服务(这种WCF服务也称为Restful服务),我们需要做一些特殊的事情。一般来说,如果我们要调用Restful服务(比如Asp.net WebAPI创建的服务),我们可以构造一个http请求,Get或者Post带有请求体,然后发送到指定的服务地址。
@ 987654321@
https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
这也适用于由 WebHttpBinding 创建的 WCF 服务。
consuming a WCF service using simple HttpClient class
但是如果我们想通过添加服务引用来消费WCF的restful服务,我们应该保持客户端和服务器之间绑定的一致性。我们应该在服务接口的自动生成方法中添加[WebGet]/[WebInvoke],它以添加服务引用的形式出现,位于Reference.cs文件中。
我们需要做的另一件事是将 Webhttpbehavior(端点行为)添加到客户端服务端点。此客户端配置以添加服务引用的形式出现,位于 app.config/web.config 的 System.ServiceModel 部分。
The remote server returned an unexpected response: (400) Bad Request. wcf
如果有什么我可以帮忙的,请随时告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多