Internet Unsecured Client and Service
The following illustration shows an example of a public, unsecured Windows Communication Foundation (WCF) client and service:
Characteristic | Description |
---|---|
Security Mode | None |
Transport | HTTP |
Binding | BasicHttpBinding in code, or the <basicHttpBinding> element in configuration. |
Interoperability | With existing Web service clients and services |
Authentication | None |
Integrity | None |
Confidentiality | None |
Service
The following code and configuration are meant to run independently. Do one of the following:
Create a stand-alone service using the code with no configuration.
Create a service using the supplied configuration, but do not define any endpoints.
Code
The following code shows how to create an endpoint with no security. By default, the BasicHttpBinding has the security mode set to None.
Uri httpUri = new Uri("http://localhost/Calculator");
// Create the ServiceHost.
ServiceHost myServiceHost = new ServiceHost(typeof(Calculator), httpUri);
// Create a binding that uses HTTP. By default,
// this binding has no security.
BasicHttpBinding b = new BasicHttpBinding();
// Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(typeof(ICalculator), b, "");
// Open the service and wait for calls.
AddMexEndpoint(ref myServiceHost);
myServiceHost.Open();
Console.Write("Listening....");
Console.ReadLine();
// Close the service when a key is pressed.
myServiceHost.Close();
Dim httpUri As New Uri("http://localhost/Calculator")
' Create the ServiceHost.
Dim myServiceHost As New ServiceHost(GetType(Calculator), httpUri)
' Create a binding that uses HTTP. By default,
' this binding has no security.
Dim b As New BasicHttpBinding()
' Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
AddMexEndpoint(myServiceHost)
myServiceHost.Open()
Console.Write("Listening....")
Console.ReadLine()
' Close the service when a key is pressed.
myServiceHost.Close()
Service Configuration
The following code sets up the same endpoint using configuration.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors />
<services>
<service behaviorConfiguration="" name="ServiceModel.Calculator">
<endpoint address="http://localhost/Calculator"
binding="basicHttpBinding"
bindingConfiguration="Basic_Unsecured"
name="BasicHttp_ICalculator"
contract="ServiceModel.ICalculator" />
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="Basic_Unsecured" />
</basicHttpBinding>
</bindings>
<client />
</system.serviceModel>
</configuration>
Client
The following code and configuration are meant to run independently. Do one of the following:
Create a stand-alone client using the code (and client code).
Create a client that does not define any endpoint addresses. Instead, use the client constructor that takes the configuration name as an argument. For example:
CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
Dim cc As New CalculatorClient("EndpointConfigurationName")
Code
The following code shows a basic WCF client that accesses an unsecured endpoint.
// Create an instance of the BasicHttpBinding.
// By default, there is no security.
BasicHttpBinding myBinding = new BasicHttpBinding();
// Create the address string, or get it from configuration.
string httpUri = "http://localhost/Calculator";
// Create an endpoint address with the address.
EndpointAddress myEndpoint = new EndpointAddress(httpUri);
// Create an instance of the WCF client. The client
// code was generated using the Svcutil.exe tool.
CalculatorClient cc = new CalculatorClient(myBinding, myEndpoint);
try
{
cc.Open();
// Begin using the calculator.
Console.WriteLine(cc.Divide(100, 2));
// Close the client.
cc.Close();
}
catch (TimeoutException tex)
{
Console.WriteLine(tex.Message);
cc.Abort();
}
catch (CommunicationException cex)
{
Console.WriteLine(cex.Message);
cc.Abort();
}
finally
{
Console.WriteLine("Closed the client");
Console.ReadLine();
}
' Create an instance of the BasicHttpBinding.
' By default, there is no security.
Dim myBinding As New BasicHttpBinding()
' Create the address string, or get it from configuration.
Dim httpUri As String = "http://localhost/Calculator"
' Create an endpoint address with the address.
Dim myEndpoint As New EndpointAddress(httpUri)
' Create an instance of the WCF client. The client
' code was generated using the Svcutil.exe tool.
Dim cc As New CalculatorClient(myBinding, myEndpoint)
Try
cc.Open()
' Begin using the calculator.
Console.WriteLine(cc.Divide(100, 2))
' Close the client.
cc.Close()
Catch tex As TimeoutException
Console.WriteLine(tex.Message)
cc.Abort()
Catch cex As CommunicationException
Console.WriteLine(cex.Message)
cc.Abort()
Finally
Console.WriteLine("Closed the client")
Console.ReadLine()
End Try
Client Configuration
The following code configures the client.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_ICalculator" >
<security mode="None">
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/Calculator/Unsecured"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ICalculator"
contract="ICalculator"
name="BasicHttpBinding_ICalculator" />
</client>
</system.serviceModel>
</configuration>