当前位置: 首页>>代码示例>>C#>>正文


C# Service类代码示例

本文整理汇总了C#中System.Web.Services.Description.Service的典型用法代码示例。如果您正苦于以下问题:C# Service类的具体用法?C# Service怎么用?C# Service使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


Service类属于System.Web.Services.Description命名空间,在下文中一共展示了Service类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Web.Services.Description;
using System.Xml;

class MyServiceClass
{
   public static void Main()
   {
      try
      {
         // Read a WSDL document.
         ServiceDescription myServiceDescription = 
            ServiceDescription.Read("MathService_CS.wsdl");
         ServiceCollection myServiceCollection = 
            myServiceDescription.Services;

         int noOfServices = myServiceCollection.Count;
         Console.WriteLine("\nTotal number of services: " + noOfServices);
         
         // Gets a reference to the service.
         Service  myOldService = myServiceCollection[0];
         Console.WriteLine("No. of ports in the service: "+
            myServiceCollection[0].Ports.Count);
         Console.WriteLine("These are the ports in the service:");
         for(int i = 0 ; i < myOldService.Ports.Count; i++)
            Console.WriteLine("Port name: " + myOldService.Ports[i].Name);
         Console.WriteLine("Service name: " + myOldService.Name);

         Service myService = new Service();
         myService.Name = "MathService";
         
         // Add the Ports to the newly created Service.
         for(int i = 0; i < myOldService.Ports.Count; i++)
         {
            string PortName = myServiceCollection[0].Ports[i].Name;
            string BindingName = myServiceCollection[0].Ports[i].Binding.Name;
            myService.Ports.Add(CreatePort(PortName,BindingName,
               myServiceDescription.TargetNamespace));
         }

         Console.WriteLine("Newly created ports -");
         for(int i = 0; i < myService.Ports.Count; i++)
            Console.WriteLine("Port name: " + myOldService.Ports[i].Name);

         // Add the extensions to the newly created Service.
         int noOfExtensions = myOldService.Extensions.Count;
         Console.WriteLine("No. of extensions: " + noOfExtensions);
         if (noOfExtensions > 0)
         {
            for(int i = 0; i < myOldService.Ports.Count; i++)
               myService.Extensions.Add(myServiceCollection[0].Extensions[i]);
         }

         // Remove the service from the collection.
         myServiceCollection.Remove(myOldService);
         
         // Add the newly created service.
         myServiceCollection.Add(myService);
         
         myServiceDescription.Write("MathService_New.wsdl");
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception: " + e.Message);
      }
   }

   public static Port CreatePort(string PortName,string BindingName,
      string targetNamespace)
   {
      Port myPort = new Port();
      myPort.Name = PortName;
      myPort.Binding = new XmlQualifiedName(BindingName,targetNamespace);

      // Create a SoapAddress extensibility element to add to the port.
      SoapAddressBinding mySoapAddressBinding = new SoapAddressBinding();
      mySoapAddressBinding.Location = 
         "http://localhost/ServiceClass/MathService_CS.asmx";
      myPort.Extensions.Add(mySoapAddressBinding);
      return myPort;
   }
}
开发者ID:.NET开发者,项目名称:System.Web.Services.Description,代码行数:83,代码来源:Service


注:本文中的System.Web.Services.Description.Service类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。