WCF service:400 bad request when accessing from local machine - c#

I have a WCF Service hosted and the client is hosted on the same server. I try to show some error message via a dialog box. its working as expected when i try to perform some action which throws that error in server but its showing Bad request when i try to work on Local machine.
Can't post the configuration file. Client machine So :) . Hosting is IIS
I found that in the controllers catch block where i am calling the wcf service whenever we are having an exception we are setting
Respose.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(execption.Message);
Does this have anything to do ?
I added this setting in the web config and i am able to see the actual message in my local machine too
<httpErrors existingResponse="PassThrough" />
Is there any other way of doing it from coding part

Related

OData, DataServiceClientException: Unauthorized

I'm developing (as a totally no-C#-guy) a web app which communicates with an external data source over OData (server B, some windows 2k12 machine).
The web app runs (is about to run) on an IIS (server A, another windows 2k12 machine), the OData source is a Dynamics NAV 2015 service (the first mentioned windows 2k12 machine, server B).
I'm developing it in VS2013 and if I'm running this locally (meaning: without publishing it, only running it within the local express IIS) it works without any problems at all.
But as soon as I publish it to the target IIS (server A) I'm getting:
An error occurred while processing this request. ---> >System.Data.Services.Client.DataServiceClientException: Unauthorized
at System.Data.Services.Client.QueryResult.ExecuteQuery()
at System.Data.Services.Client.DataServiceRequest.Execute[TElement]>(DataServiceContext context, QueryComponents queryComponents)
--- End of inner exception stack trace ---
This is esentially the piece of code which deals with the OData call:
ODataOrders.NAV odata = new ODataOrders.NAV(new Uri(serviceUri));
System.Net.NetworkCredential nc = new
System.Net.NetworkCredential(_oDataUsername, _oDataUserPassword, _oDataDomain);
odata.Credentials = nc;
DataServiceQuery<ODataOrders.Orders> query =
odata.CreateQuery<ODataOrders.Orders>("Orders");
orderList = query.Execute().ToList(); // "Unauthorized" is being thrown here
It looks like the credentials are being sent/accepted if running from local IIS. And if running from the target (production) server A, the credentials are being somehow lost/overwritten? I really don't know now...
Server A and Server B are in the same domain. My development environment isn't - I can even connect from home through VPN to my work domain and launch my local IIS and still be able to get that request done (the request goes through my local IIS and reaches the Dynamics NAV service so the data is being actually fetched).
Any help would be appreciated...
EDIT
It's Dynamics NAV 2015 CU11.
After some serious hours of reading, programming and trying I've stumbled upon a post here which gave me an idea.
And guess what? It worked - somehow...
Instead of:
System.Net.NetworkCredential(_oDataUsername, _oDataUserPassword, _oDataDomain);
I submitted only:
System.Net.NetworkCredential(_oDataUsername, _oDataUserPassword);
I turns out somehow the domain caused the call not to be authorized (401 error).

Web job throws Smtp exception on Azure website

I've a web job that is trying to send an email when it reads something from the Azure poision q.
public static void ProcessPoisonNotification(
[QueueTrigger("parkingticketnotification-poison")] ParkingTicketNotificationBO notificationBo,
TextWriter log)
{
var message = "xxx xxx xxx xxx xxx => POISON message: " + notificationBo.Dump();
Console.WriteLine(message);
log?.WriteLine(message);
PoisonEmailNotifier.SendFailureMessage(notificationBo);
}
The Email notifier is using code from the main web applicaiton, which can send emails. but the Web job throws the following exception. Is it because the web application is blocking port 25?
The Email notifier is using code from the main web applicaiton, which can send emails. but the Web job throws the following exception.
Base on my experience, as the Web application and WebJob are in the same environment, if it is worked in the Web application, it should be also worked in the Azure WebJob.
If WebJob is worked locally, please have a try to remote debug the WebJob. More details about how to remote debug the webjob, please refer to the tutorials.
Note: Click the Settings tab, and change Configuration to Debug, before it is published.
Besides,we also can use Azure SendGrid to send email easily.
That looks like Windows Socket Error Code 10013. You can find out more about it here.
Microsoft has two suggestions - you likely either have insufficient permissions or another service is bound to the target port. If you know that traffic on port 25 is not open on your host, it seems likely that this is the cause. However, you may instead have some other service listening to that port.

Channel TCP is already registered but with Infinite Loop

I have created TCP Client and Server using .Net remoting . When I call server from client i.e. at the line RemotingConfiguration.Configure("App.config", true);, I am getting error as
Remoting configuration failed with the exception 'System.Runtime.Remoting.RemotingException: The channel 'tcp' is already registered.
I have made all the settings in config file. To resolve this issue, I gave unique name to the client application. Now, I am not getting error but when I send any data from client to server,nothing happens at server or client end. If I closed the server, I get error in client application as remoting server closes the connection , which is proper behaviour. So the issue is why nothing happnes at client or server end.
RemotingConfiguration.Configure("App.config", true); line is not giving me any error, but server.Send("Hi") does not give any response back to the client. Nothing displays on server.

WCF client creation issue

I am using VS2010 + C# + .Net 4.0 + IIS 7.0 + WCF. When I create a WCF client object instance in below code, I find "check point 1" is logged, but "check point 2" is not logged. But no exception is thrown (I catch exception in the whole block of code, and "check point 3" is not output).
Any ideas how to debug further to see issues in WCF client proxy creation? What are the normal issues in WCF client proxy creation (I think the creation just need to create a local memory object to represent the WCF client object instance, no need to really communicate to remote WCF service, so should not fail normally, please correct me if I am wrong)? Maybe some configuration errors?
BtW: the code below is in a WCF service (WCF server end, running/hosted in IIS 7.0) to call another remote WCF service.
try
{
MyLogger.Write("check point 1 " );
Foo.CustomerInfo ci = new Foo.CustomerInfo();
MyLogger.Write("check point 2");
}
catch (Exception ex)
{
MyLogger.Write("check point 3");
return;
}
thanks in advance,
George
Few ideas:
Try putting logging in Foo.CustomerInfo() constructor
Try using end to end tracing with Service Trace Viewer (usually in NETFX 4.0 Tools\SvcTraceViewer.exe).
Make sure you have you service return exception details in faults so client can "see" them (e.g. serviceDebug includeExceptionDetailInFaults="true")
If you autogenerated client proxy, you can still edit it and add logging in the partial class of client proxy

Invalid or expired security context token in WCF web service

All,
I have a WCF web service (let's called service "B") hosted under IIS using a service account (VM, Windows 2003 SP2). The service exposes an endpoint that use WSHttpBinding with the default values except for maxReceivedMessageSize, maxBufferPoolSize, maxBufferSize and some of the time outs that have been increased.
The web service has been load tested using Visual Studio Load Test framework with around 800 concurrent users and successfully passed all tests with no exceptions being thrown. The proxy in the unit test has been created from configuration.
There is a sharepoint application that use the Office Sharepoint Server Search service to call web services "A" and "B". The application will get data from service "A" to create a request that will be sent to service "B". The response coming from service "B" is indexed for search. The proxy is created programmatically using the ChannelFactory.
When service "A" takes less than 10 minutes, the calls to service "B" are successfull. But when service "A" takes more time (~20 minutes) the calls to service "B" throw the following exception:
Exception Message: An unsecured or incorrectly secured fault was received from the other party. See the inner FaultException for the fault code and detail
Inner Exception Message: The message could not be processed. This is most likely because the action 'namespace/OperationName' is incorrect or because the message contains an invalid or expired security context token or because there is a mismatch between bindings. The security context token would be invalid if the service aborted the channel due to inactivity. To prevent the service from aborting idle sessions prematurely increase the Receive timeout on the service endpoint's binding.
The binding settings are the same, the time in both client server and web service server are synchronize with the Windows Time service, same time zone.
When i look at the server where web service "B" is hosted i can see the following security errors being logged:
Source: Security
Category: Logon/Logoff
Event ID: 537
User NT AUTHORITY\SYSTEM
Logon Failure:
Reason: An error occurred during logon
Logon Type: 3
Logon Process: Kerberos
Authentication Package: Kerberos
Status code: 0xC000006D
Substatus code: 0xC0000133
After reading some of the blogs online, the Status code means STATUS_LOGON_FAILURE and the substatus code means STATUS_TIME_DIFFERENCE_AT_DC. but i already checked both server and client clocks and they are syncronized.
I also noticed that the security token seems to be cached somewhere in the client server because they have another process that calls the web service "B" using the same service account and successfully gets data the first time is called. Then they start the proccess to update the office sharepoint server search service indexes and it fails. Then if they called the first proccess again it will fail too.
Has anyone experienced this type of problems or have any ideas?
Regards,
--Damian
10 mins is the default receive timeout. If you have an idled proxy for more than 10mins, the security session of that proxy is aborted by the server. Enable logging and you will see this in the diagnostics log of the server. The error message you reported fits for this behavior.
Search your system diagnostic file for "SessionIdleManager". If you find it, the above is your problem.
Give it a whirl and set the establishSecurityContext="false" for the client and the server.
Don't call the service operation in a using statement. Instead use a pattern such as...
client = new ServiceClient("Ws<binding>")
try
{
client.Operation(x,y);
client.Close();
}
catch ()
{
client.Abort();
}
I don't understand why this works but I would guess that when the proxy goes out of scope in the using statement, Close isn't called. The service then waits until receiveTimeout (on the binding) has expired and then aborts the connection causing subsequent calls to fail.
What I believe is happening here is that your channel is timing out (as you suspect).
If I understand correctly, it is not the calls to service A that are timing out, but rather to service B, before you call your operation.
I'm guessing that you are creating your channel before you call service A, rather than just in time (i.e. before calling service B). You should create the channel (proxy, service client) just before you use it like:
AResponse aResp = null;
BResponse bResp = null;
using (ServiceAProxy proxyA = new ServiceAProxy())
{
aResp = proxyA.DoServiceAWork();
using (ServiceBProxy proxyB = new ServiceBProxy())
{
bResp = proxyB.DoOtherork(aResp);
}
}
return bResp;
I believe however, that once you get over that problem (service B timing out), you'll realize that the sharepoint app's proxy (that called service A) will timeout.
To solve that, you may wish to change your service model from a request-response, to a publish-subscribe model.
With long-running services, you'll want your sharepoint app to subscribe to service A, and have service A publish its results when it is ready to do so - regardless of how long it takes.
Programming WCF Services (O'Reilly) by Juval Lowey, has a great explanation, and IDesign (Juval's company) published a great set of coding standards for WCF, as well as the code for a great Publish-Subscribe Framework.
Hope this helps,
Assaf.
I actually triggered this error just now by doing something silly. I have a unit test that modifies the system date in order to test some time-based features. And I guess the apparent time difference between when I created the context and when I called my method (because of the changes to the system date), caused something to expire.

Categories

Resources