0

I am having issues consuming a WCF service in my C# console application.

This is the code I am using to consume the service.

WCFServiceReference.WCFInterfaceClient client = new WCFInterfaceClient();

            try
            {
                WCFInterface x = client.WCFInterface();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

WCF Interface Code

public interface WCFInterface
{
    [OperationContract]
    WCFInterface WCFInterface();
}
[DataContract]
public class WCFInterface
{
    [DataMember]
    public string URI1 = "";
    [DataMember]
    public string URI2 = "";
    [DataMember]
    public string URI3 = "";
}

This then generates the following error within the service.

"Unexpected character encountered while parsing value: <. Path '', line 0, position 0."


Exception Detail:

{An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value:

<. Path '', line 0, position 0.

at Newtonsoft.Json.JsonTextReader.ParseValue()

at Newtonsoft.Json.JsonTextReader.ReadInternal()

at Newtonsoft.Json.JsonTextReader.Read()

at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)

at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)

at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)

at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)

at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)

at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value)


I can see that the error is being generated within my service, the problem I am having is that I don't know why it is being triggered.

When I run the service and invoke it manually using the Test Client in Visual Studio no error is produced and the process completes successfully.

Corey
  • 1,733
  • 4
  • 18
  • 26

2 Answers2

3

I would take a wild guess and say that your content isn't JSON, but is HTML or XML / SOAP, given the < character at 0, 0? Can you confirm what the response format is?

Did you perhaps change the response format of the service after you created the client stub?

Depending on your service type, project type, and .NET framework version, you can accomplish this several ways.

Try hardcoding the RequestFormat to JSON using the WebInvoke attribute:

 [WebInvoke(Method = "GET",
       RequestFormat = WebMessageFormat.Json,
       ResponseFormat = WebMessageFormat.Json)]

You could also make your service auto-detect and support both JSON and SOAP:

http://blogs.msdn.com/b/endpoint/archive/2010/01/18/automatic-and-explicit-format-selection-in-wcf-webhttp-services.aspx

However, for me, this isn't a solution, just a sanity check.

The real question is why does it change when you deploy?

The fact that the format changed when you deployed tells me that either the environment is configured differently, or you aren't testing the console app against your development service. In the question you said you are using the Visual Studio Test Client. Try your console app against localhost. Also, did you deploy a web.config or are you running under a web.config that already existed? Have you checked to see if there are differences?

codenheim
  • 20,467
  • 1
  • 59
  • 80
  • The response type that the WCF Service is processing is definitely JSON. – Corey May 23 '14 at 03:35
  • I am a bit lost as to why it isn't working with the console app – Corey May 23 '14 at 03:36
  • Then it shouldn't start with a <, JSON doesn't start with < so can you snoop the content? It could be an HTML / error messag as well. – codenheim May 23 '14 at 03:36
  • The content between the console app and the service is **txt/xml; charset=utf-8** using Fiddler – Corey May 23 '14 at 03:41
  • It also says that it is a **Soap Action** – Corey May 23 '14 at 03:41
  • @Corey Can you post the raw response as shown in Fiddler? SOAP is an XML protocol. – Brian Rogers May 23 '14 at 04:24
  • Yep, so that is the problem. Your service returns SOAP and your client expects JSON. – codenheim May 23 '14 at 04:26
  • It would help us see your service code and for us to know the type of service project. Are you using .NET 4 / 4.5 and Web API or an earlier data services project, or just a plain WCF service? – codenheim May 23 '14 at 04:29
  • Thanks guys. I an using .net 4. It is just a plain WCF Service. If i host the WCF Service Locally I can then connect to it correctly. The moment I host it on another machine(IIS) then I get this error. I'll try your edit now – Corey May 23 '14 at 04:36
  • Thanks for your help I eventually figured out what my issue was. – Corey May 29 '14 at 07:06
  • @Corey - Could you update us? It would be useful to know the actual cause of the symptom. – codenheim May 29 '14 at 22:07
  • @mrjoltcola I have below. – Corey May 30 '14 at 07:22
0

I ended up figuring out what was going on.

My console app was invoking a WCF service which in turn was consuming a web api.

The web api was returning an error msg in a format which was not what my WCF service was expecting.

By following this answer I was able to fix my issue, I know it seems pretty different to my original question, but that was what was being received by my console app. By tracing the error I was able to determine that the error was actually being generated by my WCF service which up until then had been working correctly.

407 Proxy Authentication Required

Community
  • 1
  • 1
Corey
  • 1,733
  • 4
  • 18
  • 26