开发者

Calling a WCF Service without a Service reference

开发者 https://www.devze.com 2023-02-13 16:19 出处:网络
I have a project A which has a Service reference to a WCF Service. I want to invoke the service in project B without a Service reference.

I have a project A which has a Service reference to a WCF Service. I want to invoke the service in project B without a Service reference. From what I understand, the Service reference is just a way to generate the proxy and config and is not used at run-time. I copied the proxy class and the node from project A to project B.

Can I just create an instance of the proxy class in project B and expect i开发者_Go百科t to directly use the config. items and connect to the service without anything else? (I cannot try this right now)


Short answer yes.

As long as you have the interface, a way to connect to the service, you can create a channel and talk to service without having the reference.

The reference simply makes it easier for you to develop against.


Look into creating channels from your service contracts with ChannelFactory.


Here is a working copy. For me it works fine and returns List

private List<MyClass> GetAllSiteDetailsJSON(string language)
{

   Uri address = 
            new Uri(@"http://weburlpath/MyService/MyService.svc/GetAllList/"
                                                                   + language);
   HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
   request.Method = "GET";
   request.ContentType = "application/x-www-form-urlencoded";

   using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
   {
       StreamReader reader = new StreamReader(response.GetResponseStream());
       string jSon = reader.ReadToEnd();
       reader.Close();

       JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
       List<MyClass> result = jsSerializer.Deserialize<List<MyClass>>(jSon);
       return result;
   }
}

And Here is the class

public class MyClass
{
    public string ID { get; set; }
    public string Name { get; set; }       
    public List<Location> Locations { get; set; }
}

public class Location
{
    public string Region { get; set; }
    public string Country { get; set; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消