且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在visual studio 2015中创建Web服务时在控制台应用程序中添加服务引用

更新时间:2023-01-04 09:56:23

这应该很容易。请参阅 - 致电(消费)使用C#和VB.Net的控制台应用程序中的Web服务(ASMX) [ ^ ]。






为此,您必须先将两个项目保持在同一解决方案中。



1.创建一个控制台应用程序名称作为ClientApp

现在从Solution Explorer - 右键单击​​Solution

从可用选项中选择ADD - >新项目/现有项目(如果您已经创建了一个项目)并添加了Web服务项目。



2.现在首先通过创建启动来运行Web服务项目获取服务URL的项目。它只不过是运行Web服务时打开的浏览器中的URL。



3.现在来到你的控制台应用程序,右键单击控制台应用程序,然后从选项中选择添加服务引用。



粘贴我们在步骤2中找到的URL,然后单击go。它将列出Web服务中存在的所有方法。



在点击确定之前为ServiceReference1命名空间提供一些好名字



在此之后你必须在控制台应用程序中添加命名空间;



喜欢



使用ServiceReference1 
Class Program {

static void Main(string [] args)
{
}





}



现在你可以从控制台应用程序调用web服务方法





您可以参考此链接获取更多信息,

如何在控制台应用程序中访问Web服务 [ ^ ]





谢谢,

Ejaz Waquif


创建您的Web服务项目,然后将您的控制台项目添加到同一解决方案中,这样您现在应该在解决方案中有两个项目。使控制台项目成为启动项目。在Visual Studio中右键单击您的asmx文件,然后选择在浏览器中查看。这将启动Web服务项目并在浏览器中启动它,这将确保它正在运行,因此您可以添加对它的引用。从浏览器复制URL并在控制台应用程序中添加一个web reference \ service引用到该URL。



最后你可能需要设置两个项目都是在启动代码时启动,右键单击VS中的解决方案并选择设置启动项目并选择多个启动项目并选择Web服务和控制台项目。



现在当您按F5时,控制台应用程序将启动,Web服务也将启动(不是在浏览器中,它只是在后台监听),允许控制台应用程序使用它。

Hi i started learning web service and i tried creating a sample in visual studio 2015 but i cant consume it and i cant add service reference in the console application can someone explain

What I have tried:

i tried adding the console application to the web application in the same solution and when debugged it and UI is not displayed

That should be easy. Refer - Call (Consume) Web Service (ASMX) in Console application using C# and VB.Net[^].


Hi,

To do so you have to first keep both the projects in same solution.

1.Create a Console Application name as ClientApp
Now from the Solution Explorer - right click on Solution
From the available options select ADD -> New Project/Existing Project(If you already created a project) and add the web service project.

2.Now first run you web service project by creating it the startup project to get the service URL .It is nothing but the URL which is present in browser that open when you run the web service.

3.Now come to your console application, Right click on console applicatioa and select "Add Service Reference" From the options.

Paste the URL that we found in step 2 and click go. It will list down all the method present in web service.

Before clicking ok provide some good name to ServiceReference1 namespace

After this you have to add the namespace in console app;

like

using ServiceReference1
Class Program{

static void Main(string[] args)
{
}



}

Now you can call the web service methods from console app


You can refer this link for more information,
How to Access a Web Service in Console Application[^]


Thanks,
Ejaz Waquif


Create your web service project, then add your console project to the same solution so you should now have two projects in the solution. Make the console project the start-up project. Right click your asmx file in Visual Studio and select "View in browser". That will start the web service project and launch it in a browser, that will ensure it is running so you can add a reference to it. Copy the url from the browser and in your console app add a web reference\service reference to that url.

Finally you might need to set that both projects are to start when you launch the code, so right click the Solution in VS and select "Set Startup Projects" and select the "Multiple startup projects" and select both the web service and the console projects.

Now when you press F5 the console app will launch and the web service will also launch (not in a browser though, it will just be listening in the background) allowing the console app to use it.