0

I’m trying to reach a 3th party API with an asp.net core (C#) app. I get stuck on the connection already. The owners of the API don ‘t know much about C# and can’t help much. For test purposes I wrote the code directly in the Homecontroller ‘s index method.

public IActionResult Index()
{
            // create an instance of the binding
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
            binding.MaxReceivedMessageSize = int.MaxValue;
            binding.Security.Mode = (BasicHttpSecurityMode)SecurityMode.Transport;
            try
            {
                using (var client = new V3PortClient(binding, new EndpointAddress(new Uri("https://platform.com/Webservices/V3?wsdl")))) 
                {
            //Sent a message in the platform
            var result = client.sendMsgAsync(accesscode, userIdentifier, title, body, senderIdentifier, 0, 0, false);
                        ViewBag.Message = result.Result;
                }
            }
            catch (Exception ex)
            {
                // handle exception
                ViewBag.Message = ex;
            }

            return View();
}

This code gives this error on the closing bracket of “using (var client = new V3PortClient…”

System.ServiceModel.CommunicationObjectFaultedException: The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state. at System.ServiceModel.Channels.CommunicationObject.System.ServiceModel.IAsyncCommunicationObject.CloseAsync(TimeSpan timeout) at System.ServiceModel.Channels.CommunicationObject.CloseAsyncInternal(TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout) at System.ServiceModel.ClientBase`1.System.ServiceModel.ICommunicationObject.Close(TimeSpan timeout) I have this in an Reference class (it’s a part from the class) public V3PortClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress)

I’m almost sure the password (accesscode) doesn’t have to be included in the binding because it’s been asked for every time a task is being started see below ‘sendMsgAsync(string accesscode,…”

[System.ServiceModel.OperationContractAttribute(Action="https://platform.com/Webservices/V3#sendMsg", ReplyAction="*")]
    [System.ServiceModel.XmlSerializerFormatAttribute(Style=System.ServiceModel.OperationFormatStyle.Rpc, SupportFaults=true, Use=System.ServiceModel.OperationFormatUse.Encoded)]
    [return: System.ServiceModel.MessageParameterAttribute(Name="return")]
    System.Threading.Tasks.Task<object> sendMsgAsync(string accesscode, string userIdentifier, string title, string body, string senderIdentifier, object attachments, int coaccount, bool copyToLVS);

I am on completely new ground here so any help will be appreciated.

Thanks in advance

  • Are you sure the 3rd party service is working ? Because it say "communication state faulted" . I saw an similar issue https://stackoverflow.com/questions/2763592/the-communication-object-system-servicemodel-channels-servicechannel-cannot-be . – firatt_ Jan 24 '23 at 14:32
  • Yes that works. My powershell version does perform the task. – ElectricalStorm Jan 24 '23 at 14:51
  • In my humble opinion, you can firstly try to use tools like postman to call the API and checking the parameters/url you used which calling the API successfully. then try to use httpClient in c# to test the connection... I didn't find any document about `V3PortClient` – Tiny Wang Jan 25 '23 at 02:00
  • `cV3PortClient` is a Method from the 3rd party API. I generated a .cs file with svcutil. V3PortClient comes as this Method among others in that c# file. _public V3PortClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress) { }_ – ElectricalStorm Jan 25 '23 at 15:56

1 Answers1

0

I got it. I added a 'Service Reference' in Visual Studio. Then choose 'choose WCF Web service' and type in the Web service link. This video shows how and what https://www.youtube.com/watch?v=ycKnYOlQDEE.

Thanks for the input.