简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM