Home > OS >  invoking WCF service with custombinding from a .Net standard client
invoking WCF service with custombinding from a .Net standard client

Time:08-01

I have a legacy WCF service with custom binding. I want to invoke this service from a .Net Standard 2.0 client. I have added nuget packages system.servicemodel.primitives, system.servicemodel.security and system.servicemodel.security. Below is my channel factory setup

        var binding = new CustomBinding();
        var security = SecurityBindingElement.CreateUserNameOverTransportBindingElement();
        security.IncludeTimestamp = true;
        binding.Elements.Add(security);

        binding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Default, Encoding.UTF8));
        binding.Elements.Add(new HttpsTransportBindingElement { MaxBufferSize = MaxBufferSize, MaxReceivedMessageSize = MaxReceivedMessageSize });
        var headers = new Dictionary<string, string>
        {
            {"Ocp-Apim-Subscription-Key","xxxxxxxxxxxxxxxxxxxxxxx"}
        };
        var behaviour = new AddHttpHeaderMessageEndpointBehavior(headers);
        ChannelFactory<ITaskService> cf = new ChannelFactory<ITaskService>(binding, new EndpointAddress("https://api-xxl.yyy.com/cvg/fg/swer/v1"));
        cf.Endpoint.EndpointBehaviors.Add(behaviour);
        cf.Credentials.UserName.UserName = "username";
        cf.Credentials.UserName.Password = "password";

I get the below error

"HTTP request is unauthorized with client authentication scheme 'Anonymous'. The authentication header received from the server was 'AzureApiManagementKey"

I am able to successfully invoke the API from a .Net Framework client. The only difference is

        binding.Elements.Add(new TextMessageEncodingBindingElement(**MessageVersion.Soap12**, Encoding.UTF8));

Can someone advise me what is the solution?

CodePudding user response:

The difference between these two code is TextMessageEncodingBindingElement MessageVersion attribute values.

The picture above is the default, the object returned the same as the Soap12WSAddressing10. Below need to make some changes. You can refer to this docs for further info.

CodePudding user response:

The solution to pass header is implementing IClientMessageInspector: BeforeSendRequest() when we implement IEndpointBehavior when we create behavior for channel factory

  • Related