简体   繁体   中英

Consuming a WCF Service with a Console Application

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.

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM