WinFrom调用WebServer服务

首先创建一个winfrom程序,然后再引用右键添加服务引用,点击左下角高级,在弹出的界面点击添加Web引用,在弹出界面URL输入引用的Web服务地址


本例引用的是天气预报的Web服务,更改Web引用名,点击添加引用即可。

后台调用;

WeatherInterface.WeatherWebService weather = new WeatherInterface.WeatherWebService();

再利用对象调用里面的方法即可。

注:Web项目后台调用Web服务方法一样。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
完整的服务端及客户端调用程序,在win7+ vs2015环境运行通过. 一、说明 1、创建winfrom应用程序;(或者是控制台项目) 2、在项目中添加一个WCF服务,并实现服务; 3、在需要启动WebService服务的地方启动该服务即可; 二、代码如下: 1、新建一个WCF服务——定义服务接口    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]     public interface ICalculator     {         [OperationContract]         double Add(double n1, double n2);     } 2、新建一个WCF服务——实现服务 public class CalculatorService : ICalculator     {         public double Add(double n1, double n2)         {             return n1 + n2;         }     } 3、添加完WcF服务后会在应用程序配置文件中有入下节点                             <!--TestServer.ICalculator服务定义的接口,根据自己定义进行修改-->                                                                   <baseAddresses> <!--这个是要发布的服务地址,可以进行修改-->                                   </baseAddresses>                   4、在要启动服务的地方启动服务监听   public frmMain() { InitializeComponent(); } private void frmMain_Load(object sender, EventArgs e) { try { //打开服务创建监听,并开始监听消息 ServiceHost serviceHost = new ServiceHost(typeof(Service1));//需要using System.ServiceModel; serviceHost.Open(); label1.Text = "服务启动正常"; } catch (Exception ex) { label1.Text = ex.Message; } } 5、下面可以在客户端通过上面的服务地址”http://xxx.xxx.xxx.xx:8733/test/Service1/“对服务进行调用 到这步就实现在控制台中实现webService的发布。
OWIN(Open Web Interface for .NET)是一个开放的Web应用程序接口,可以让你在.NET应用程序中使用不同的Web服务器和Web框架。OWIN允许你将Web服务器和应用程序分离开来,这样你可以使用不同的Web服务器来运行你的应用程序,而无需修改应用程序的代码。 在Winform应用程序中调用OWIN,你需要先安装 Microsoft.Owin.Hosting NuGet 包,然后在代码中使用以下代码启动OWIN: ```csharp using Microsoft.Owin.Hosting; using System; namespace WinformApp { public partial class Form1 : Form { private IDisposable _webApp; public Form1() { InitializeComponent(); } private void btnStart_Click(object sender, EventArgs e) { string baseAddress = "http://localhost:9000/"; _webApp = WebApp.Start<Startup>(url: baseAddress); MessageBox.Show("OWIN web server started at " + baseAddress); } private void btnStop_Click(object sender, EventArgs e) { _webApp.Dispose(); MessageBox.Show("OWIN web server stopped"); } } } ``` 在上面的代码中,我们使用WebApp.Start方法来启动OWIN服务器。我们还提供了一个名为Startup的类,该类用于配置OWIN应用程序。在这个类中,你可以添加中间件和路由,以确保请求被正确地路由到你的应用程序中。 ```csharp using Microsoft.Owin; using Owin; [assembly: OwinStartup(typeof(WinformApp.Startup))] namespace WinformApp { public class Startup { public void Configuration(IAppBuilder app) { // Add middleware and configure routes here } } } ``` 在上面的代码中,我们使用OwinStartup属性告诉OWIN要使用Startup类来配置应用程序。在Configuration方法中,我们可以添加中间件和路由来处理请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值