1

I dispose a WCF Service Implemented with WCF 4.6.1 Framework containing an Endpoint binded with WS2007HttpBinding. My purpose is to implement a .NET Core client Side application that calls this service. When i tried to use CustomBinding, it generates this error :

System.InvalidOperationException : 'The CustomBinding on the ServiceEndpoint with contract 'IWSTrust13Sync' lacks a TransportBindingElement. Every binding must have at least one binding element that derives from TransportBindingElement.'

public class Program
{
    public static void Main(string[] args)
    {
        Message requestmessage = Message.CreateMessage(
          MessageVersion.Soap12WSAddressing10,
          "http://test/test",
           "This is the body data");

        var binding = new CustomBinding();
        var endpoint = new EndpointAddress(new Uri("http://servicetest.service.svc"));
        var channelFactory = new ChannelFactory<ServiceSTS.IWSTrust13Sync>(binding, endpoint);
        var serviceClient = channelFactory.CreateChannel();
        var result = serviceClient.Trust13ValidateAsync(requestmessage);
        channelFactory.Close();

        Console.WriteLine(result.Status);
    }
}

Is there a replacement of WS2007HttpBinding in .NET Core ?

Notice that i should not change any things in the service because it is in use by other applications

kaisfarza
  • 73
  • 1
  • 7

1 Answers1

1

As the error shows, you lack TransportBindingElement. Binding consists of BingingElement, a custom binding without bingingelement makes no sense. If you want to use custom binding , you should add binging element into it.

BindingElement[] elements = new BindingElement[]
        {

                       new TextMessageEncodingBindingElement(),
          new HttpTransportBindingElement()
        };
        CustomBinding binding = new CustomBinding(elements);
                    using (ChannelFactory<ICalculatorService> channelFacoty = new ChannelFactory<ICalculatorService>(binding, new EndpointAddress("http://localhost:4000/calculator")))
        {
            ICalculatorService cal = channelFacoty.CreateChannel();
           Console.WriteLine( cal.Add(1, 3));
            Console.Read();
        }

But since you are using ws2007httpbinging in server side, why not directly use ws2007httpbinding in your client side. Using ws2007httpbinging, you needn't add bingingelements into it.(They have been added).

Below is a simple use of ws2007httpbinding.

  WS2007HttpBinding binding2 = new WS2007HttpBinding();
        using (ChannelFactory<ICalculatorService> channelFacoty = new ChannelFactory<ICalculatorService>(binding2, new EndpointAddress("http://localhost:4000/calculator")))
        {
            ICalculatorService cal = channelFacoty.CreateChannel();
           Console.WriteLine( cal.Add(1, 3));
            Console.Read();
        }

But if your service side has special configuration of ws2007httpbinging, you had better config your ws2007httpbinging of client side according to its service side configuration.

If your service side has publish its metadata, you could add reference to it and directly use the client to call the service. https://www.dotnetforall.com/adding-wcf-service-reference-client/ For svc, the default service metadata address is its address + WSDL,

for example , you metadata address is http://servicetest.service.svc?wsdl

You could also use App.config to configure your client.

<client>
   <endpoint address="http://localhost:4000/calculator" binding="ws2007HttpBinding"
  contract="ServiceInterface.ICalculatorService" name="cal" />
</client>

And then , you could directly create the client as follows,

 using (ChannelFactory<ICalculatorService> channelFacoty = new ChannelFactory<ICalculatorService>("cal"))
        {
            ICalculatorService cal = channelFacoty.CreateChannel();
           Console.WriteLine( cal.Add(1, 3));
            Console.Read();
        }

Please configure your client according to the endpoint configuration of your server side. Mine is

<service name="Service.CalculatorService"  >
             <endpoint address="http://localhost:4000/calculator"  binding="ws2007HttpBinding"   contract="ServiceInterface.ICalculatorService"></endpoint>
  </service>
Ackelry Xu
  • 756
  • 3
  • 6
  • I'm using a .NET CORE Client Application so i dont have no App.config file and no WS2007HttpBinding. but your Solution gives me an other way of reflexion. Thnaks – kaisfarza Jan 11 '19 at 13:04