Aracılığıyla paylaş


Meta Verileri Nasıl Alınır ve Uyumlu Bir Hizmet Nasıl Uygulanır?

Genellikle, aynı kişi hizmetleri tasarlamaz ve uygulamaz. Birlikte çalışma uygulamalarının önemli olduğu ortamlarda sözleşmeler Web Hizmetleri Açıklama Dili'nde (WSDL) tasarlanabilir veya açıklanabilir ve bir geliştiricinin sağlanan sözleşmeye uygun bir hizmet uygulaması gerekir. Var olan bir hizmeti Windows Communication Foundation'a (WCF) geçirmek ancak kablo biçimini korumak da isteyebilirsiniz. Buna ek olarak, çift yönlü sözleşmeler, çağıranların bir geri arama sözleşmesi de uygulamasını gerektirir.

Böyle durumlarda, sözleşmenin gereksinimlerini karşılamak için uygulayabileceğiniz yönetilen bir dilde hizmet sözleşmesi arabirimi oluşturmak için ServiceModel Meta Veri Yardımcı Programı Aracı (Svcutil.exe) (veya eşdeğer bir araç) kullanmanız gerekir. Genellikle ServiceModel Meta Veri Yardımcı Programı Aracı (Svcutil.exe), bir kanal fabrikası veya WCF istemci türüyle ve doğru bağlamayı ve adresi ayarlayan bir istemci yapılandırma dosyasıyla birlikte kullanılan bir hizmet sözleşmesini almak için kullanılır. Oluşturulan yapılandırma dosyasını kullanmak için bunu bir hizmet yapılandırma dosyası olarak değiştirmeniz gerekir. Hizmet sözleşmesini de değiştirmeniz gerekebilir.

Verileri almak ve uyumlu bir hizmet uygulamak için

  1. Bir kod dosyası oluşturmak için meta veri dosyalarına veya meta veri uç noktasına karşı ServiceModel Meta Veri Yardımcı Programı Aracı'nı (Svcutil.exe) kullanın.

  2. Çıkış kodu dosyasının, System.ServiceModel.ServiceContractAttribute özniteliğiyle işaretlenmiş ilgi alanı arabirimini (birden fazla olması durumunda) içeren bölümünü arayın. Aşağıdaki kod örneği, ServiceModel Meta Veri Yardımcı Programı Aracı (Svcutil.exe)tarafından oluşturulan iki arabirimi gösterir. İlki (ISampleService), uyumlu bir hizmet oluşturmak için uyguladığınız hizmet sözleşmesi arabirimidir. İkincisi (ISampleServiceChannel), hem hizmet sözleşmesi arabirimini hem de System.ServiceModel.IClientChannel genişleten ve istemci uygulamasında kullanılmak üzere istemci kullanımına yönelik bir yardımcı arabirimdir.

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    [System.ServiceModel.ServiceContractAttribute(
      Namespace="http://microsoft.wcf.documentation"
    )]
    public interface ISampleService
    {
    
        [System.ServiceModel.OperationContractAttribute(
          Action="http://microsoft.wcf.documentation/ISampleService/SampleMethod",
          ReplyAction="http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
        )]
        [System.ServiceModel.FaultContractAttribute(
          typeof(microsoft.wcf.documentation.SampleFault),
          Action="http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
        )]
        string SampleMethod(string msg);
    }
    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
    public interface ISampleServiceChannel : ISampleService, System.ServiceModel.IClientChannel
    {
    }
    
  3. WSDL tüm işlemler için bir yanıt eylemi belirtmezse, oluşturulan işlem sözleşmelerinde ReplyAction özelliği (*) joker karakterine ayarlanmış olabilir. Bu özellik ayarını kaldırın. Aksi takdirde, hizmet sözleşmesi meta verilerini uyguladığınızda meta veriler bu işlemler için dışarı aktarılamaz.

  4. Arabirimi bir sınıf üzerinde uygulayın ve hizmeti barındırın. Örneğin, Hizmet Sözleşmesini Uygulama, nasıl yapılır veya Örnek bölümünde aşağıdaki basit uygulamaya bakın.

  5. ServiceModel Meta Veri Yardımcı Programı Aracı'nın (Svcutil.exe) oluşturduğu istemci yapılandırma dosyasında, <istemci> yapılandırma bölümünü <hizmetleri> yapılandırma bölümüyle değiştirin. (Oluşturulan bir istemci uygulaması yapılandırma dosyası örneği için aşağıdaki "Örnek" bölümüne bakın.)

  6. <hizmetleri> yapılandırma bölümünde, hizmet uygulamanız için <hizmetleri> yapılandırma bölümünde bir name özniteliği oluşturun.

  7. Hizmet name özniteliğini hizmet uygulamanız için yapılandırma adı olarak ayarlayın.

  8. Uygulanan hizmet sözleşmesini kullanan uç nokta yapılandırma öğelerini hizmet yapılandırması bölümüne ekleyin.

Örnek

Aşağıdaki kod örneğinde, meta veri dosyalarına karşı ServiceModel Meta Veri Yardımcı Programı Aracı (Svcutil.exe) çalıştırılarak oluşturulan bir kod dosyasının çoğunluğu gösterilmektedir.

Aşağıdaki kod şunları gösterir:

  • Uygulandığında sözleşme gereksinimlerine (ISampleService) uyan hizmet sözleşmesi arabirimi.

  • Hizmet sözleşmesi arabirimini ve System.ServiceModel.IClientChannel'ı genişleten, bir istemci uygulamasında (ISampleServiceChannel) kullanılmak üzere tasarlanmış yardımcı istemci arabirimi.

  • System.ServiceModel.ClientBase<TChannel>'yi genişleten ve bir istemci uygulamasında (SampleServiceClient) kullanılmak üzere tasarlanmış yardımcı sınıf.

  • Hizmetten oluşturulan yapılandırma dosyası.

  • Basit bir ISampleService hizmeti uygulaması.

  • İstemci tarafı yapılandırma dosyasının hizmet tarafı sürümüne dönüştürülmesi.

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.42
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

[assembly: System.Runtime.Serialization.ContractNamespaceAttribute("http://microsoft.wcf.documentation", ClrNamespace="microsoft.wcf.documentation")]

namespace microsoft.wcf.documentation
{
    using System.Runtime.Serialization;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class SampleFault : object, System.Runtime.Serialization.IExtensibleDataObject
    {

        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;

        private string FaultMessageField;

        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string FaultMessage
        {
            get
            {
                return this.FaultMessageField;
            }
            set
            {
                this.FaultMessageField = value;
            }
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(
  Namespace="http://microsoft.wcf.documentation"
)]
public interface ISampleService
{

    [System.ServiceModel.OperationContractAttribute(
      Action="http://microsoft.wcf.documentation/ISampleService/SampleMethod",
      ReplyAction="http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
    )]
    [System.ServiceModel.FaultContractAttribute(
      typeof(microsoft.wcf.documentation.SampleFault),
      Action="http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
    )]
    string SampleMethod(string msg);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ISampleServiceChannel : ISampleService, System.ServiceModel.IClientChannel
{
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class SampleServiceClient : System.ServiceModel.ClientBase<ISampleService>, ISampleService
{

    public SampleServiceClient()
    {
    }

    public SampleServiceClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
    {
    }

    public string SampleMethod(string msg)
    {
        return base.Channel.SampleMethod(msg);
    }
}
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ISampleService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:8080/SampleService" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_ISampleService" contract="ISampleService"
                name="BasicHttpBinding_ISampleService" />
        </client>
    </system.serviceModel>
</configuration>
// Implement the service. This is a very simple service.
class SampleService : ISampleService
{
  public string SampleMethod(string msg)
  {
    Console.WriteLine($"The caller said: \"{msg}\"");
    return "The service greets you: " + msg;
  }
}
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ISampleService" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
            allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
            maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
            useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <services>
      <service
          name="Microsoft.WCF.Documentation.SampleService">
        <endpoint address="http://localhost:8080/SampleService" binding="basicHttpBinding"
            bindingConfiguration="BasicHttpBinding_ISampleService" contract="Microsoft.WCF.Documentation.ISampleService"
            />
      </service>
    </services>
  </system.serviceModel>
</configuration>

Ayrıca bkz.