Protocol exception error - c#

I'm trying to call soap service in script task but getting this following error. I've added a serviceReference through wsdl url. I don't have idea about what this error is referring to. However i'm able to get the data in SOAPUI
"The header 'ReliableMessaging' from the namespace 'http://sap.com/xi/XI/Message/30' was not understood by the recipient of this message, causing the message to not be processed. This error typically indicates that the sender of this message has enabled a communication protocol that the receiver cannot process. Please ensure that the configuration of the client's binding is consistent with the service's binding."
below is the code i'm using
EndpointAddress endPointAddress = new EndpointAddress("https://sapnxcci.intel.com:8220/XISOAPAdapter/MessageServlet?senderParty=&senderService=MC4510&receiverParty=&receiverService=&interface=Segments&interfaceNamespace=http://intel.com/xi/Intel-MDM/Project/Segment");
SegmentsClient client = new SegmentsClient(BindingUtility.GetInitiativesBinding(), endPointAddress);
client.ClientCredentials.UserName.UserName = "sys_idwcons";
client.ClientCredentials.UserName.Password = "abdc";
SegmentResponse results = client.ListSegments(new SegmentRequest() { SegmentName = "" });
In GetInitiativesBinding contains the following.
public static BasicHttpBinding GetInitiativesBinding()
{
BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;
binding.MaxReceivedMessageSize = 2147483647;
binding.MaxBufferSize = 2147483647;
binding.ReceiveTimeout = TimeSpan.FromMinutes(5);
binding.SendTimeout = TimeSpan.FromMinutes(5);
return binding;
}
and my App.Config file looks like this
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="SegmentsBinding" />
<binding name="SegmentsBinding1">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://sapnxcci.intel.com:8420/XISOAPAdapter/MessageServlet?senderParty=&senderService=MC4510&receiverParty=&receiverService=&interface=Segments&interfaceNamespace=http%3A%2F%2Fintel.com%2Fxi%2FIntel-MDM%2FProject%2FSegment"
binding="basicHttpBinding" bindingConfiguration="SegmentsBinding"
contract="ServiceReference1.Segments" name="HTTP_Port" />
<endpoint address="https://sapnxcci.intel.com:8220/XISOAPAdapter/MessageServlet?senderParty=&senderService=MC4510&receiverParty=&receiverService=&interface=Segments&interfaceNamespace=http%3A%2F%2Fintel.com%2Fxi%2FIntel-MDM%2FProject%2FSegment"
binding="basicHttpBinding" bindingConfiguration="SegmentsBinding1"
contract="ServiceReference1.Segments" name="HTTPS_Port" />
</client>
</system.serviceModel>
</configuration>

I think it's because the return message from PI contains the tags.
tags are only useful when you have a PI - PI connection.
Reference: https://archive.sap.com/discussions/thread/1839924

Related

C# NetTcpBinding Client bindings from other config file

I have a client which is a windows application called Windows.exe. I have a C# class library called ServiceFacade.dll and it has a config file called ServiceFacade.dll.config. In ServiceFacade.dll.config, I have client side bindings like below
<system.serviceModel>
<client>
<endpoint address="net.tcp://localhost:5000/MyService"
binding="netTcpBinding"
contract="IMyService"
name="NetTcpBinding_MyService"/>
</client>
</system.serviceModel>
In ServiceFacade.dll, I have code like below to create proxy
NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_MyService");
ChannelFactory<IMyService> chn = new ChannelFactory<IMyService>(binding);
IMyService service = chn.CreateChannel();
Windows.exe calls ServiceFacade.dll to make service calls.
But below line is looking for NetTcpBinding_MyService in Windows.exe.config instead of ServiceFacade.dll.config
How to make below line to see NetTcpBinding_MyService in ServiceFacade.dll.config but not Windows.exe.config ?
NetTcpBinding binding = new NetTcpBinding("NetTcpBinding_MyService");
You definitely can copy the configuration to the ServiceFacade.dll.config from the Windows.exe.config. I would rather create a service endpoint manually than to copy the configuration when I call the service by using ChannelFactory.
Client-side.
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("https://vabqia969vm:21011/");
NetTcpBinding binding = new NetTcpBinding();
binding.OpenTimeout = new TimeSpan(0, 10, 0);
binding.MaxReceivedMessageSize = 2147483647;
binding.ReaderQuotas.MaxStringContentLength = 2147483647;
ChannelFactory<ITestService> factory = new ChannelFactory<ITestService>(binding, new EndpointAddress(uri));
ITestService service = factory.CreateChannel();
var result1 = service.GetData(34);
Console.WriteLine(result1);
}
}
//The service contract is shared between the client-side and the server-side.
[ServiceContract]
public interface ITestService
{
[OperationContract]
string GetData(int id);
}
Wish it is useful to you.
I did like below.
I added below in ServiceFacade.dll.config
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="netTcpBindingConfiguration"
closeTimeout="00:10:00"
openTimeout="00:10:00"
receiveTimeout="00:10:00"
sendTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:5000/MyService"
binding="netTcpBinding"
contract="IMyService"
name="NetTcpBinding_MYService"
bindingConfiguration="netTcpBindingConfiguration" />
</client>
</system.serviceModel>
In ServiceFacade.dll, I have code like below to create proxy
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
Configuration config = ConfigurationManager.OpenExeConfiguration(path);
ConfigurationChannelFactory<IMyService> chn =
new ConfigurationChannelFactory<IMyService>(
"NetTcpBinding_MyService",
config,
new EndpointAddress("net.tcp://localhost:5000/MyService"));
IMyService iMyService = chn.CreateChannel();

System.ServiceModel.ProtocolException: 'The content type application/xml

I have added a service reference to the third party webservice and on making a WCF call from my console application, I get the error message below:
System.ServiceModel.ProtocolException: 'The content type application/xml; charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8). If using a custom encoder, be sure that the IsContentTypeSupported method is implemented properly. The first 610 bytes of the response were: '<?xml version="1.0"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:CancelServiceResponse">
<soapenv:Body>
<tns:CancelServiceResponse>
<CancelServiceResult>
<Status_Code>FAILED</Status_Code>
<Status_Description>Service_ID= not found.</Status_Description>
<Order_ID></Order_ID>
</CancelServiceResult>
</tns:CancelServiceResponse>
</soapenv:Body>
</soapenv:Envelope>
Config file as below:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IB2BService">
<security mode="TransportWithMessageCredential">
<message clientCredentialType="UserName" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://thirdpartyendpointaddress"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IB2BService"
contract="b2bService.IB2BService" name="BasicHttpBinding_IB2BService" />
</client>
</system.serviceModel>
Can someone advise, what needs to be done to fix this issue? I have searched all over SO and have not been able to find how to overcome this.
There is no problem the code snippets seem to me. One thing must be noted is to make sure the binding is consistent between the server and the client and has the correct service endpoint. On the client end, we could generate the configuration with Adding service reference tool. What I consider the best reply is to give you an example of invocation service with TransportWithMessageCredential.
Server end (10.157.13.69. Console application)
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("https://localhost:11011");
BasicHttpBinding binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;
binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
using (ServiceHost sh = new ServiceHost(typeof(MyService), uri))
{
sh.AddServiceEndpoint(typeof(IService), binding, "");
ServiceMetadataBehavior smb;
smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
{
smb = new ServiceMetadataBehavior()
{
HttpsGetEnabled = true
};
sh.Description.Behaviors.Add(smb);
}
sh.Credentials.UserNameAuthentication.UserNamePasswordValidationMode = System.ServiceModel.Security.UserNamePasswordValidationMode.Custom;
sh.Credentials.UserNameAuthentication.CustomUserNamePasswordValidator = new CustUserNamePasswordVal();
Binding mexbinding = MetadataExchangeBindings.CreateMexHttpsBinding();
sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
sh.Opened += delegate
{
Console.WriteLine("Service is ready");
};
sh.Closed += delegate
{
Console.WriteLine("Service is clsoed");
};
sh.Open();
Console.ReadLine();
sh.Close();
Console.ReadLine();
}
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string SayHello();
}
public class MyService : IService
{
public string SayHello()
{
return $"Hello Stranger,{DateTime.Now.ToLongTimeString()}";
}
}
internal class CustUserNamePasswordVal : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
if (userName != "jack" || password != "123456")
{
throw new Exception("Username/Password is not correct");
}
}
}
Bind the certificate to the port.
netsh http add sslcert ipport=0.0.0.0:11011 certhash=6e48c590717cb2c61da97346d5901b260e983850 appid={ED4CE60F-6B2E-4EE6-828F-C1A6A1B12565}
Client end (calling service by adding service reference)
var client = new ServiceReference1.ServiceClient();
client.ClientCredentials.UserName.UserName = "jack";
client.ClientCredentials.UserName.Password = "123456";
try
{
var result = client.SayHello();
Console.WriteLine(result);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
Configuration file(auto-generated)
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService">
<security mode="TransportWithMessageCredential" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://10.157.13.69:11011/" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService" contract="ServiceReference1.IService"
name="BasicHttpBinding_IService" />
</client>
</system.serviceModel>
Feel free to let me know if there is anything I can help with.

WCF Dynamic Binding - How to specify end points?

I'm trying to bind my WCF client using code instead of app.config as I'll need to change host IP addresses for different deployment.
This is my app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICX" maxReceivedMessageSize="1073741824" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://127.0.0.1/CX/CX.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ICX" contract="CXService.ICX" name="WSHttpBinding_ICX">
<identity>
<servicePrincipalName value="host/SilverStar" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
and this is my code:
public static void StartUp()
{
XmlDictionaryReaderQuotas quota = new XmlDictionaryReaderQuotas();
quota.MaxArrayLength = 2147483647;
quota.MaxBytesPerRead = 2147483647;
quota.MaxDepth = 2147483647;
quota.MaxNameTableCharCount = 2147483647;
quota.MaxStringContentLength = 2147483647;
EndpointAddress addr = new EndpointAddress(new Uri("http://127.0.0.1/CX/CX.svc"));
WSHttpBinding binding1 = new WSHttpBinding();
binding1.Name = "WSHttpBinding_ICX";
binding1.MaxReceivedMessageSize = 1073741824;
binding1.ReaderQuotas = quota;
// Globals.CXClient is the client object
Globals.CXClient = new CXService.CXClient(binding1, addr);
// This line does not compile! Endpoint is read-only!!
Globals.CXClient.Endpoint = new ServiceEndpoint(new ContractDescription("CXService.ICX"), (Binding)binding1, addr);
}
The last line of the code does not compile as .EndPoint is read-only property.
Please help.
try this:
Globals.CXClient.Endpoint.Address = new System.ServiceModel.EndpointAddress("your url here");
This is how I did it.
Globals.CXClient.Endpoint.Contract = new ContractDescription("CXService.ICX");
Globals.CXClient.Endpoint.Binding = binding1;
Globals.CXClient.Endpoint.Address = addr;
However, I encountered another problem. I tried to replace the part:
<identity>
<servicePrincipalName value="host/SilverStar" />
</identity>
by using the following code:
addr.Identity = new SpnEndpointIdentity("host/SilverStar");
This one again fails to compile because addr.Identity is read-only.
I also tried this:
EndpointAddress addr = new EndpointAddress(new Uri("http://127.0.0.1/CX/CX.svc"), new SpnEndpointIdentity("host/SilverStar"), ???);
but the last parameter is an AddressHeaderCollection and I have no idea what should be placed in there.
Please help again. Thanks.

Security header in WSDL C#

I have to make a client for a WSDL. But I keep getting a
"No Security header in message but required by policy."
Here is my code
USImportoerService service = new USImportoerService();
X509Certificate2 cert = new X509Certificate2(certPath, PASSWORD, X509KeyStorageFlags.MachineKeySet);
service.ClientCertificates.Add(cert);
var result = ser.getUSKoeretoej();
And heres the App.config
<configuration>
<system.serviceModel>
<bindings>
<customBinding>
<binding name="USImportoerService_Port">
<security authenticationMode="CertificateOverTransport" securityHeaderLayout="LaxTimestampLast" includeTimestamp="true"
enableUnsecuredResponse="true">
</security>
<textMessageEncoding messageVersion="Soap11" />
<httpsTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="HTTPS TO WSDL"
binding="customBinding" bindingConfiguration="USImportoerService_Port"
contract="ServiceReferenceUSImportoer.USImportoerServiceType"
name="Port1" />
</client>
</system.serviceModel>
Hope someone can help
Use HttpClient to call SOAP Service
USImportoerService service = new USImportoerService();
X509Certificate2 cert = new X509Certificate2(certPath, PASSWORD, X509KeyStorageFlags.MachineKeySet);
HttpClientHandler handler = new HttpClientHandler();
handler.ClientCertificates.Add(cert);
HttpClient client = new HttpClient(handler);
// Call service using httpClient
//var result = ser.getUSKoeretoej();
How to call SOAP Service using httpClient
Client to send SOAP request and receive response

Endpoint and WSHttpBinding programmatically

I'm trying to move the endpoint and wshttpbinding configuration to a c# file so I can change the endpoint address at runtime (select from dropdown, process etc). However after creating a WsHttpBinding object, EndpointAddress object and passing them to my client. It will throw the following exception:
System.ServiceModel.FaultException: The caller was not authenticated
by the service
This is the same exception I get if the user credentials are incorrect. However they haven't changed from using the Web.config file to creating these config options programmatically.
Web.config (works):
<binding name="myService" maxReceivedMessageSize="2147483647">
<readerQuotas
maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<security mode="TransportWithMessageCredential">
<transport clientCredentialType="None" />
<message clientCredentialType="UserName" establishSecurityContext="false" />
</security>
</binding>
<client>
<endpoint
address="https://address/myservice.svc"
binding="wsHttpBinding"
bindingConfiguration="myService"
contract="MyService.IMyService"
name="myService"
/>
</client>
MyService service = new MyService();
service.username = "user";
service.password = "pass";
//Success
Programmatically (does not work):
WSHttpBinding wsHttp = new WSHttpBinding();
wsHttp.MaxReceivedMessageSize = 2147483647;
wsHttp.ReaderQuotas.MaxDepth = 2147483647;
wsHttp.ReaderQuotas.MaxStringContentLength = 2147483647;
wsHttp.ReaderQuotas.MaxArrayLength = 2147483647;
wsHttp.ReaderQuotas.MaxBytesPerRead = 2147483647;
wsHttp.ReaderQuotas.MaxNameTableCharCount = 2147483647;
wsHttp.Security.Mode = SecurityMode.TransportWithMessageCredential;
wsHttp.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
wsHttp.Security.Message.ClientCredentialType = MessageCredentialType.UserName;
wsHttp.Security.Message.EstablishSecurityContext = false;
EndpointAddress endpoint = new EndpointAddress("https://address/myservice.svc");
MyService service = new MyService(wsHttp, endpoint);
service.username = "user";
service.password = "pass";
//System.ServiceModel.FaultException: The caller was not authenticated by the service
I've tried following tutorials / looking at answers but I can't figure it out.
My solution
Keep the binding the same.
Change the endpoint so there is no address
<client>
<endpoint
binding="wsHttpBinding" bindingConfiguration="myService"
contract="MyService.IMyService" name="myService" />
</client>
Change endpoint at run time by changing the Uri object and pass the endpoint name your service as the first argument
Uri uri = new Uri("https://address/myservice.svc");
var address = new EndpointAddress(uri);
service= new MyService("myService", address);
service.username = "user";
service.password = "pass";
You can remove EVERYTHING in your app.config - so there is no binding or interface info.
Your runtime code is this:
//create an endpoint address
var address = new EndpointAddress("http://localhost:51353/EmployeeService.svc");
//create a WSHttpBinding
WSHttpBinding binding = new WSHttpBinding();
//create a channel factory from the Interface with binding and address
var channelFactory = new ChannelFactory<IEmployeeService>(binding, address);
//create a channel
IEmployeeService channel = channelFactory.CreateChannel();
//Call the service method on the channel
DataSet dataSet = channel.SelectEmployees();
Just for reference, here is my app.config:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
</configuration>
/************ You can dumb this down to this: ********************************/
var factory = new ChannelFactory<IEmployeeService>(new WSHttpBinding());
var channel = factory.CreateChannel(new EndpointAddress("http://localhost:51353/EmployeeService.svc"));

Categories