0
votes

my WCF Rest makes a request to another Rest Api, but it returns the following error:

System.Net.WebException: Connection is terminated: Unexpected sending error. ---> System.IO.IOException: Authentication failed because the remote party closed the transport stream. \ R \ n in System.Net.Security.SslState.StartReadFrame (Byte [] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) \ r \ n in System.Net.Security.SslState.StartReceiveBlob (Byte [] buffer, AsyncProtocolRequest asyncRequest) \ r \ n in System.Net.Security.SslState.CheckCompletionBeforeNextReceive (ProtocolToken message, AsyncProtocolRequest asyncRequest) \ r \ n in System. Net.Security.SslState.StartSendBlob (Byte [] incoming, Int32 count, AsyncProtocolRequest asyncRequest) \ r \ n in System.Net.Security.SslState.ForceAuthentication (Boolean receiveFirst, Byte [] buffer, AsyncProtocolRequest asyncRequest) \ r \ n System.Net.Security.SslState.ProcessAuthentication (LazyAsyncResult lazyResult) \ r \ n in System.Net.TlsStream.CallProcessAuthentication (Object state) \ r \ n in System.Threading.ExecutionContext.RunInternal (ExecutionContext executionContext, ContextCallback callback, Object state , B oolean preserveSyncCtx) \ r \ n in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) \ r \ n in System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state) \ r \ n in System.Net.TlsStream.ProcessAuthentication (LazyAsyncResult result) \ r \ n in System.Net.TlsStream.Write (Byte [] buffer, Int32 offset, Int32 size) \ r \ n in System.Net.PooledStream .Write (Byte [] buffer, Int32 offset, Int32 size) \ r \ n in System.Net.ConnectStream.WriteHeaders (Boolean async) \ r \ n --- End of stack trace of internal exception --- \ r \ n in System.Net.WebClient.UploadDataInternal (Uri address, String method, Byte [] data, WebRequest & request) \ r \ n in System.Net.WebClient.UploadString (Uri address, String method, String data) \ r \ n in BS.Control.Save.Send (String body, String to)

where I have the functions is in a library c # called BS, in the, class "Send", and in this I do this:

the

json= {"user":"test","pass":"12345","message":"test 1","number":"12345678"}

using (var WC = new WebClient())
{
    JObject response = new JObject();
    ResponseD Result = new ResponseD();
    try
    {
        WC.Headers.Add(HttpRequestHeader.Authorization,"Bearer MyCode");
        WC.Headers.Add(HttpRequestHeader.ContentType,"application/json");
        string baseurl = "url-where receive the post";
        response = JObject.Parse(WC.UploadString(new Uri(baseurl), "POST", json));
    }
    catch(Exception ex)
    {
        Result.id = "Error";
        Result.result = ex.ToString();
    }  
} 

This Exception is present in my local PC, in my IIS server and even in an Azure APPservice .... in my IIS server I configured an SSL to where I have the WCF, but with SSL my WCF continues with the same problem and does not send response...

i was make a console application where i use the same library as my WCF Rest and in this there are no problems, the "response = JObject.Parse (WC.UploadString (new Uri (baseurl)," POST ", json));" Receive answer without problems .... recive a beautiful Json with the information of the id assigned to the request, time issued among other things, but this the only thing I need is the ID and from a ConsoleApp Everything works well ....

The Json of response is:

{
"id": "Error",
    "result":
 the exception mentioned above and the location in Line where the problem is located and this is where the WC.uploadstring is
}

Can someone help me with this problem?

1
you are missing and ending } for your using() { have you used the debugger to step thru the code..?MethodMan
If I did not have it in my code, I could not have obtained the exception, I already corrected it in my query. thanks for your contribution ;)samuel Sai
It is not clear the difference between the code working and not working. If the location of the difference is a different PC then check following : 1) Is the time stamp of dll same on both PCs. 2) Are the PCs same type. There are build options like x86 and 16/32/64 bit modes where application will work on one PC and not all PCs.jdweng
thanks for your suggestion, but as I mentioned before it was a TLS compatibility problem, the solution put it at the beginning of the consult. ;)samuel Sai
I moved your solution to a community wiki answer.Cœur

1 Answers

0
votes

Solution by OP.

It turns out that the server where the Rest service is hosted is not compatible with TLS1.0.

So I added this code before uploadstring and it works perfectly on my IIS server.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
                                     | SecurityProtocolType.Tls12
                                     | SecurityProtocolType.Tls11
                                     | SecurityProtocolType.Tls;