• wcf


     

    1>客户端程序调用wcf,在程序中配置服务器的节点

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.ServiceModel.Description;
    
    namespace ConsoleApplication1
    {
        [ServiceContract]
        public interface IService
        {
            [OperationContract]
            [WebGet]
            string EchoWithGet(string s);
    
            [OperationContract]
            [WebInvoke]
            string EchoWithPost(string s);
        }
    
        public class Service : IService
        {
            public string EchoWithGet(string s)
            {
                return "You said " + s;
            }
    
            public string EchoWithPost(string s)
            {
                return "You said " + s;
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                WebServiceHost host = new WebServiceHost(typeof(Service), new Uri("http://localhost:7900/"));
                try
                {
                    ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), "");
                    host.Open();
                    using (ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), "http://localhost:7900"))
                    {
                        cf.Endpoint.Behaviors.Add(new WebHttpBehavior());
    
                        IService channel = cf.CreateChannel();
    
                        string s;
    
                        Console.WriteLine("Calling EchoWithGet via HTTP GET: ");
                        s = channel.EchoWithGet("Hello, world");
                        Console.WriteLine("   Output: {0}", s);
    
                        Console.WriteLine("");
                        Console.WriteLine("This can also be accomplished by navigating to");
                        Console.WriteLine("http://localhost:7900/EchoWithGet?s=Hello, world!");
                        Console.WriteLine("in a web browser while this sample is running.");
    
                        Console.WriteLine("");
    
                        Console.WriteLine("Calling EchoWithPost via HTTP POST: ");
                        s = channel.EchoWithPost("Hello, world");
                        Console.WriteLine("   Output: {0}", s);
                        Console.WriteLine("");
                    }
    
                    Console.WriteLine("Press <ENTER> to terminate");
                    Console.ReadLine();
    
                    host.Close();
                }
                catch (CommunicationException cex)
                {
                    Console.WriteLine("An exception occurred: {0}", cex.Message);
                    Console.Read();
                    host.Abort();
                }
            }
        }
    }

    2 客户端程序调用wcf,在配置文件中配置服务器的节点

    1>新建“asp.net 空 web 应用程序”》在新建“wcf 服务”,直接将源码发布到iis中,

    http://localhost/wcftest/MyFistService.svc?wsdl

    2>在浏览器中打开以上地址,出现一下提示信息:

    “若要测试此服务,需要创建一个客户端,并将其用于调用该服务。可以使用下列语法,从命令行中使用 svcutil.exe 工具来进行此操作“

    svcutil.exe http://localhost/wcftest/MyFistService.svc?wsdl

    3>安装2中的提示操作,在vs的命令窗口中执行以上操作,生成output.config和Service1.cs两个文件,将config中的配置节点服务到

    自己的客户端程序中去,Service1.cs文件直接复制到客户端程序中。

    程序员的基础教程:菜鸟程序员

  • 相关阅读:
    从屏幕刷新频率到Unity VSync
    TextMesh Pro不能显示中文的解决办法是创建字贴图,常用汉字3500+特殊字符
    50 个 Chrome Developer Tools 必备技巧
    Unity发布到Google Play应用上架流程
    Unity Shader入门
    Unity2019游戏框架搭建第一季C# 核心知识与简易框架搭建 + Unity2019 游戏框架搭建第二季:UI 模块与资源模块持续精进
    TextMesh Pro不能显示中文的解决办法是创建字贴图,常用汉字3500
    permanently
    UE4地编大型开放世界~制作烘焙全流程
    Unity高级游戏地编案例
  • 原文地址:https://www.cnblogs.com/guohu/p/4469016.html
Copyright © 2020-2023  润新知