简单的WCF REST客户端 - 添加服务引用错误?客户端、错误、简单、REST

2023-09-03 05:38:28 作者:我拼了命爱ヤ你拼了命躲

我想作一个简单的WCF世界,你好客户端,可以连接到一个WCF REST服务。

I want to make a simple WCF Hello world client which could connect to a WCF REST service.

不过,我已经得到了以下错误:

But I've got the following error:

无法找到该ServiceModel客户端配置节中引用合同'ServiceReference1.IService1默认终结点元素。这可能是因为没有配置文件找到您的应用程序,或者是因为没有终点的元素匹配这份合同可以中找到客户端的元素。

"Could not find default endpoint element that references contract 'ServiceReference1.IService1' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

我所做的:

- 我创建了一个名为WCFerror的新项目与WCF服务应用程序模板

-I created a new project called "WCFerror" with the "WCF Service Application" template

- 我的web.config文件是这样的: http://pastebin.com/KEGqRgPr

-My web.config is like this: http://pastebin.com/KEGqRgPr

- 我的服务接口也很简单:

-My service interface is also simple:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet(UriTemplate = "GetData?value={value}", ResponseFormat = WebMessageFormat.Json)]
    string GetData(int value);
}

- 我创造了一个新的控制台应用程序。

-I created a new Console Application.

- 我开始了我的WCFerror服务(通过开始调试)一个新的实例,它是主持,我尝试过了在Web浏览器 (如:的http://本地主机:58475 / Service1.svc /的GetData值= 4 ),它工作得很好

-I started a new instance of my WCFerror service (via "Start Debugging"), it is hosted, I tried it out in a web browser ( like: http://localhost:58475/Service1.svc/GetData?value=4 ), it worked fine.

- 然后我添加了一个服务引用到控制台应用程序(地址是:的http://本地主机:58475 / Service1.svc ),并在该背景下, SvcUtil工具生成的客户端code和一个app.config - 而是一个空的app.config

-Then I added a service reference to the Console Application (the address was: http://localhost:58475/Service1.svc) and in the background, the svcutil generated the Client code, and an app.config - but an empty app.config!

- 所以我的客户不工作:

-So my client not works:

ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
Console.WriteLine(client.GetData(4));

- 我试图通过运行命令提示符这样的SvcUtil工具:

-I tried to run the svcutil via the command prompt like this:

svcutil.exe /language:cs /out:GeneratedProxy.cs /config:app.config http://localhost:58475/Service1.svc

但它产生同样的空的app.config。

But it generates the same empty app.config.

我做了什么错? :(

推荐答案

添加服务引用使用WSDL或WS-MetadataExchange的。这两种是SOAP构造。 REST不具有元数据的标准。你将不得不自己滚动的消息,preferably使用框架。你看HttpClient的是新的Web API的一部分? 它可以通过的NuGet

Add Service Reference uses WSDL or WS-MetadataExchange. Both of these are SOAP constructs. REST does not have a metadata standard. You will have to roll the messages yourself, preferably using a framework. Have you looked at HttpClient that is part of the new Web API? Its available via Nuget

 
精彩推荐