Dinesh
Dinesh

Reputation: 27

invoking WCF service with custombinding from a .Net standard client

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?

Upvotes: 3

Views: 1139

Answers (2)

Dinesh
Dinesh

Reputation: 27

We need to pass a behavior for the channel factory by implementing IEndpointBehavior with header details in constructor. Implement IClientMessageInspector and instantiate it from the custom behavior. The BeforeSendRequest() of IClientMessageInspector can be used to set the header for the request.

Upvotes: 0

Jiayao
Jiayao

Reputation: 568

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.

Upvotes: 1

Related Questions