13 Mart 2018 Salı

BasicHttpBinding Sınıfı

Giriş
WCF ile kullanılabilecek kanal tiplerinden bir tanesi. SOAP tarzı iletişim için kullanılır.

EndPoint Oluşturmak İçin Basit Bir Örnek
Şöyle yaparız
[ServiceContract]
public interface IMyService
{
  [OperationContract]
  void Test();
}

public class MyService : IMyService
{
  public void Test()
  {
    throw new NotImplementedException();
  }
}

static void Main()
{
  var endpointAddress = new Uri("http://127.0.0.1:5555/MyService");
  using (var host = new ServiceHost(typeof(MyService), endpointAddress))
  {
    var basicHttpBinding = new BasicHttpBinding();
    host.AddServiceEndpoint(typeof(IMyService), basicHttpBinding, string.Empty);
    host.Open();
  }
}
İstemci Config Dosyası
Şöyle yaparız.
<basicHttpBinding>
  <binding name="MyBinding" closeTimeout="00:02:00" openTimeout="00:02:00"
    receiveTimeout="00:02:00" sendTimeout="00:02:00"/>
</basicHttpBinding>
<netTcpBinding>
  <binding name="CrossDomainBinding">
    <security mode="Message">
      <message clientCredentialType="Certificate"/>
    </security>
  </binding>
</netTcpBinding>
<client>
  <endpoint address="net.tcp://MyService.svc"
  binding="netTcpBinding" bindingConfiguration="CrossDomainBinding"
    behaviorConfiguration="CrossDomainBehavior" contract="MyContract" name="MyBinding">
  <identity>
    <certificate encodedValue="CERTIFICATEENCODEDVALUE" />
  </identity>
  </endpoint>
</client>
WebService Çağrısı Yapmak İçin Basit Bir Örnek
Şöyle yaparız.
private const string AxlEndpointUrlFormat = "https://{0}:8443/axl/";

var basicHttpBinding = new BasicHttpBinding(...);
...
var endpointUrl = ...;
var endpointAddress = new EndpointAddress(endpointUrl);
var client = new MyClient(basicHttpBinding, endpointAddress);
Constructor
Şöyle yaparız.
System.ServiceModel.Channels.Binding
  binding = new System.ServiceModel.BasicHttpBinding();
Şöyle yaparız.
var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
Buffer Alanları
Şöyle yaparız.
binding.MaxBufferSize = 65536;
binding.MaxBufferPoolSize = 524288;
binding.MaxReceivedMessageSize = 65536;
Encoding Alanları
Şöyle yaparız.
binding.MessageEncoding = System.ServiceModel.WSMessageEncoding.Text;
binding.TextEncoding = System.Text.Encoding.UTF8;
binding.TransferMode = System.ServiceModel.TransferMode.Buffered;
Name Alanı
Şöyle yaparız.
binding.Name = "authentication";
Proxy Alanları
Şöyle yaparız.
binding.AllowCookies = false;
binding.BypassProxyOnLocal = false;
binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
binding.UseDefaultWebProxy = true;
Quota Alanları
Şöyle yaparız.
binding.ReaderQuotas.MaxDepth = 32;
binding.ReaderQuotas.MaxStringContentLength = 8192;

binding.ReaderQuotas.MaxArrayLength = 16384;
binding.ReaderQuotas.MaxBytesPerRead = 4096;
binding.ReaderQuotas.MaxNameTableCharCount = 16384;
Timeout Alanları
Şöyle yaparız.
binding.CloseTimeout = System.TimeSpan.Parse("00:01:00");
binding.OpenTimeout = System.TimeSpan.Parse("00:01:00");
binding.ReceiveTimeout = System.TimeSpan.Parse("00:10:00");
binding.SendTimeout = System.TimeSpan.Parse("00:01:00");
Security Alanı
Örnek - Basic
Şöyle yaparız.
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
Örnek - None
Şöyle yaparız.
binding.Security.Mode = BasicHttpSecurityMode.None;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;

binding.Security.Transport.Realm = "";
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
Örnek - NTLM
Şöyle yaparız.
BasicHttpBinding basicHttpBinding = new BasicHttpBinding();

basicHttpBinding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;

basicHttpBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Ntlm;

EndpointAddress endpoint = new EndpointAddress("http://myservice");

var factory = new ChannelFactory<IMyService>(basicHttpBinding, endpoint);
CredentialCache myCredentialCache = new CredentialCache();

NetworkCredential myCreds = new NetworkCredential("username", "password", "domain");
myCredentialCache.Add("ContoscoMail", 45, "NTLM", myCreds);
factory.Credentials.Windows.ClientCredential = 
         myCredentialCache.GetCredential("ContosoMail", 45, "NTLM");

var client = factory.CreateChannel(); 

// ... use the webservice


Hiç yorum yok:

Yorum Gönder