C# 添加对控制台应用程序的 Web 服务引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/887914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
add a web service reference to a console app
提问by raklos
Im creating a simple web service in a console app. (PersonService) this is my Program.cs below
我在控制台应用程序中创建了一个简单的 Web 服务。(PersonService) 这是我下面的 Program.cs
im trying to add a service reference to a different console app (PersonClient) how can i do this? i tried adding it by right clicking, add service reference, pointing to the refernce etc... but it wont work.
我正在尝试向不同的控制台应用程序 (PersonClient) 添加服务引用,我该怎么做?我尝试通过右键单击添加它,添加服务引用,指向引用等...但它不会工作。
[DataContract]
public class Person
{
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }
}
[ServiceContract]
public interface IPersonLookup
{
[OperationContract]
Person GetPerson(int identifier);
}
public class PersonService : IPersonLookup
{
public PersonService()
{
}
public Person GetPerson(int identifier)
{
Person p = new Person();
p.FirstName="Jane";
p.LastName="Doe";
return p;
}
}
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(PersonService)))
{
WSHttpBinding binding = new WSHttpBinding();
host.AddServiceEndpoint(typeof(IPersonLookup), binding, "http://localhost:9090/PersonService");
host.Open();
Console.WriteLine("Listening....");
Console.ReadLine();
}
}
}
采纳答案by RichardOD
You need to read about WCF MEX Endpoints. Here's a blog post that may help.
回答by Marc Gravell
You have two console exes, one which runs a ServiceHost
- is that correct? Run the server console without debugging; then in the IDE add the WCF reference to the url. It should work, but it needs the server (your second console exe) to be running when you query the mex.
您有两个控制台 exe,其中一个运行一个ServiceHost
- 对吗?在不调试的情况下运行服务器控制台;然后在 IDE 中添加对 url 的 WCF 引用。它应该可以工作,但是当您查询 mex 时,它需要运行服务器(您的第二个控制台 exe)。
回答by Wayne Hartman
When you added the webservice reference, you defined the namespace and 'class name' for the service. You must either add the namespace reference ("using FooNameSpace;") or use the fully qualified class name of the service ("FooNameSpace.BarClass ws = new FooNameSapce.BarClass()");
添加 webservice 引用时,您定义了服务的命名空间和“类名”。您必须添加命名空间引用(“using FooNameSpace;”)或使用服务的完全限定类名(“FooNameSpace.BarClass ws = new FooNameSapce.BarClass()”);
回答by chakra
Solution:
解决方案:
- Create a console application using visual studio.
- Right click on the project and click on "Add Service Reference...".
- On the window, you will find the "Advanced" button at the bottom.
- Click on the button and it will open service reference settings window. It has a button at bottom called "Add Web Reference".
- 使用 Visual Studio 创建控制台应用程序。
- 右键单击该项目,然后单击“添加服务引用...”。
- 在窗口的底部,您会找到“高级”按钮。
- 单击按钮,它将打开服务引用设置窗口。它在底部有一个名为“添加 Web 引用”的按钮。