WCF Web服务客户端没有端点侦听
本文关键字:端点 Web 服务 客户端 WCF | 更新日期: 2023-09-27 18:04:43
我试图创建一个WCF web服务与JSON和消费与客户端在asp.net我的WCF web服务器在IIS上运行,我已经检查了浏览器,得到JSON响应。
这是服务器web。配置文件:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<!--<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>-->
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfServiceApp.Service1">
<endpoint address="../Service1.svc" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior >
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
</customHeaders>
</httpProtocol>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
之后,我在asp.net中创建了一个Web应用程序客户端来使用WCF Web服务。我已经在客户端添加了WCF web服务引用,但是
Visual Studio 2012未更新客户端web.config
。我发现了一些东西在stackoverflow为我的客户端web.config
客户机web . config<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<!--<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>-->
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webby">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint address="http://localhost/WCFService/Service1.svc" name="Service1" binding="webHttpBinding"
contract="ServiceReference1.IService1" behaviorConfiguration="webby"/>
</client>
</system.serviceModel>
</configuration>
从客户端调用服务
protected void Button1_Click(object sender, EventArgs e)
{
string result;
string input = tbInput.Text;
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
try
{
result = client.GetData(input);
lbResult.Text = result;
client.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
但是当我尝试从Web服务读取时,得到异常
无终点http://localhost/WCFService/Service1.svc/GetData可以接受消息。这通常是由不正确的地址或SOAP操作引起的。详情请参见InnerException(如果存在)。
内部异常:远程服务器返回了一个错误:(404)Not发现。"}
我怀疑这是一个配置问题在我的网页。
谢谢,Ashok
您的服务使用WebHttpBinding
。这基本上是REST
服务。所以你不能添加Service Reference
,因为REST
服务不公开任何元数据,你需要通过HTTP
动词,如GET
或POST
等来查询资源。
Try like:
var req = (HttpWebRequest)WebRequest.Create("your endpoint");
var data_to_send = Encoding.ASCII.GetBytes("some data");
using (var _temp = req.GetRequestStream())
{
_temp.Write(data_to_send, 0, data_to_send.Length);
}
var res = req.GetResponse();
您也可以选择:
var req = new HttpClient().PostAsync("your url", new StringContent(JsonConvert.SerializeObject("your params")));
还可以在endpointBehavior
中启用一些属性来捕获更具体的异常细节。
我阅读了更多关于webHttpBinding的信息,REST发现这样的服务的客户端不能通过添加服务引用来创建。
下面是调用WCF服务的示例代码:(我发现很容易),响应是JSON格式,您需要以不同的方式提取它(截至目前我不知道如何做到这一点)
Thanks to http://www.codeproject.com/Articles/275279/Developing-WCF-Restful-Services-with-GET-and-POST
string url = "http://localhost:50327/Service1.svc/data/"+input;
string strResult = string.Empty;
// declare httpwebrequet wrt url defined above
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
// set method as post
webrequest.Method = "GET";
// set content type
webrequest.ContentType = "application/json"; //x-www-form-urlencoded”;
// declare & read response from service
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
// set utf8 encoding
Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
// read response stream from response object
StreamReader loResponseStream = new StreamReader
(webresponse.GetResponseStream(), enc);
// read string from stream data
strResult = loResponseStream.ReadToEnd();
// close the stream object
loResponseStream.Close();
// close the response object
webresponse.Close();
// assign the final result to text box
result = strResult;
lbResult.Text = strResult;
我认为这不会总是配置问题:一旦我有同样的问题,但我忘记了servicecontract
,所以服务在服务器上运行,但无法访问它。以下是一些检查点:
1)找到你的.svc
文件,右键单击它,在浏览器中选择选项视图,将给你确切的本地URL
。
2)给你的合同参数一个完整的值,例如,而不是Contract1添加完整的命名空间x.y.Contract1
3)检查你是否在IIS服务器上放置了最新的dll并重置了应用程序池
4)检查OperationContract、ServiceContract标签设置是否正确