none
The Address property on ChannelFactory.Endpoint was null. The ChannelFactory's Endpoint must have a valid Address specified.

    Question

  • Hi,
      I am getting above error:

    Client:

    <system.serviceModel>
       <behaviors>
       <endpointBehaviors>
         <behavior name="WebHttpBinding_IRestService">
           <webHttp />
         </behavior>
       </endpointBehaviors>
       </behaviors>
       <bindings>
       <customBinding>
        <binding name="WebHttpBinding_IRestService">
         <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
          messageVersion="Soap12" writeEncoding="utf-8">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
           maxBytesPerRead="4096" maxNameTableCharCount="16384" />
         </textMessageEncoding>
        </binding>
       </customBinding>
      </bindings>
      <client>
       <endpoint binding="customBinding" bindingConfiguration="WebHttpBinding_IRestService"
        contract="ServiceReference1.IRestService" name="WebHttpBinding_IRestService" address="http://localhost/WCFServices/RESTWcf"/>
      </client>
     </system.serviceModel>

    Server:

    <?xml version="1.0"?>
    <configuration>
     <system.serviceModel>
      <behaviors>
       <serviceBehaviors>
        <behavior name="RestServiceBehavior">
         <serviceMetadata httpGetEnabled="true"/>
         <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
       </serviceBehaviors>
       <endpointBehaviors>
        <behavior name="REST">
         <webHttp/>
        </behavior>
       </endpointBehaviors>
      </behaviors>
      <services>
       <service behaviorConfiguration="RestServiceBehavior" name="RestService">
        <endpoint address="" binding="webHttpBinding" contract="IRestService" behaviorConfiguration="REST">
         <identity>
          <dns value="localhost"/>
         </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost/WCFServices/RESTWcf" />          
              </baseAddresses>
            </host>

          </service>
      </services>
     </system.serviceModel>
     <system.web>
      <compilation debug="true"/></system.web></configuration>

    Iservice:

    [ServiceContract]
    public interface IRestService
    {
        [OperationContract]
        [WebInvoke(UriTemplate = "/Search?name={name}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json,Method="GET")]
        string GetString(string name);
        [OperationContract]
        [WebGet]
        string GetGreetingAsFunction(string name);

        [OperationContract]
       // [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "/")]
        string xmldata(string value);

        [OperationContract]
        //[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/")]
        string jsondata(string value);
    }

    Service:


        public string GetString(string Name)
        {
            return "HI , " + Name + " Welcome to simplest REST Service";
        }
        public string GetGreetingAsFunction(string Name)
        {
            return "HI , " + Name + " Welcome to simplest REST Service";
        }
        public string xmldata(string value)
        {
            return "your xml string is" + value;
        }

        public string jsondata(string value)
        {
            return "your  json string is" + value;
        }
    }

    Now i want host this service in web application there i have one button click event i want call a method what ever i want , but it is getting error

    i added refreance and using obj instance of service.

    in Button_click():

            RestServiceClient objService = new RestServiceClient();

            string str = "Hello";
            lblRest.Visible = true;
            lblRest.Text = objService.GetString(str);   

    I am getting this error:

    The Address property on ChannelFactory.Endpoint was null.  The ChannelFactory's Endpoint must have a valid Address specified.

    Can you help for this

    Tuesday, June 26, 2012 1:11 PM

Answers

  • See the above code, in the server config file, you have not included the namespace for your service name and contract in endpoint. The service is using webHttpBinding which illustrates that it is a REST service. But see your client config file, it is using a custom binding, and it is using SOAP message encoding, so the client and service does not match. As for channel factory, it is not recommended to use REST services, since REST service does not based on SOAP, you could use HttpWebRequest to call your REST service method. You could refer to this post.

    http://go4answers.webhost4life.com/Example/address-property-191740.aspx


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework


    Friday, June 29, 2012 3:50 AM
  • Your service element name and endpoint contract attribute values should be fully qualified. They should be namespace.RestService and namespace.IRestService.

    Your server configuration and building of a REST WCF service is correct. Your method of accessing the REST WCF Service is not the right way as you are accessing the service using SOAP whereas REST service doesnt work with SOAP.

    Please find some sample code below that would access your REST WCF service from a client using WebRequest class

    private string UseHttpWebApproach<T>(string serviceUrl, string resourceUrl, string method, T requestBody)
            {
                string responseMessage = null;            
                var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
                if (request != null)
                {
                    request.ContentType = "application/xml";                
                    request.Method = method;
                }
                
                if (method == "POST" && requestBody != null)
                {
                    byte[] requestBodyBytes;                
                    requestBodyBytes = ToByteArrayUsingDataContractSer(requestBody);                
                    request.ContentLength = requestBodyBytes.Length;
                    using (Stream postStream = request.GetRequestStream())
                        postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
                }
    
                if (request != null)
                {
                    var response = request.GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream responseStream = response.GetResponseStream();
                        if (responseStream != null)
                        {
                            var reader = new StreamReader(responseStream);
    
                            responseMessage = reader.ReadToEnd();
                        }
                    }
                    else
                    {
                        responseMessage = response.StatusDescription;
                    }
                }
                return responseMessage;
            }
    private static byte[] ToByteArrayUsingDataContractSer<T>(T requestBody)
            {
                byte[] bytes = null;
                var serializer1 = new DataContractSerializer(typeof(T));            
                var ms1 = new MemoryStream();            
                serializer1.WriteObject(ms1, requestBody);
                ms1.Position = 0;
                var reader = new StreamReader(ms1);
                bytes = ms1.ToArray();
                return bytes;
            }


    To use the above method i do it using the code below:

     string serviceBaseUrl = "http://localhost/restService";
                string resourceUrl = "/xmldata";
                string method = "POST";
    
                TempData["xml"] = UseHttpWebApproach<string>(serviceBaseUrl, resourceUrl, method, "hello world");


    Now you can monitor your request using Fiddler to see how your request and response would look like.


    Friday, June 29, 2012 3:46 PM

All replies

  • See the above code, in the server config file, you have not included the namespace for your service name and contract in endpoint. The service is using webHttpBinding which illustrates that it is a REST service. But see your client config file, it is using a custom binding, and it is using SOAP message encoding, so the client and service does not match. As for channel factory, it is not recommended to use REST services, since REST service does not based on SOAP, you could use HttpWebRequest to call your REST service method. You could refer to this post.

    http://go4answers.webhost4life.com/Example/address-property-191740.aspx


    Please mark the replies as answers if they help or unmark if not. If you have any feedback about my replies, please contact msdnmg@microsoft.com Microsoft One Code Framework


    Friday, June 29, 2012 3:50 AM
  • Your service element name and endpoint contract attribute values should be fully qualified. They should be namespace.RestService and namespace.IRestService.

    Your server configuration and building of a REST WCF service is correct. Your method of accessing the REST WCF Service is not the right way as you are accessing the service using SOAP whereas REST service doesnt work with SOAP.

    Please find some sample code below that would access your REST WCF service from a client using WebRequest class

    private string UseHttpWebApproach<T>(string serviceUrl, string resourceUrl, string method, T requestBody)
            {
                string responseMessage = null;            
                var request = WebRequest.Create(string.Concat(serviceUrl, resourceUrl)) as HttpWebRequest;
                if (request != null)
                {
                    request.ContentType = "application/xml";                
                    request.Method = method;
                }
                
                if (method == "POST" && requestBody != null)
                {
                    byte[] requestBodyBytes;                
                    requestBodyBytes = ToByteArrayUsingDataContractSer(requestBody);                
                    request.ContentLength = requestBodyBytes.Length;
                    using (Stream postStream = request.GetRequestStream())
                        postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
                }
    
                if (request != null)
                {
                    var response = request.GetResponse() as HttpWebResponse;
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        Stream responseStream = response.GetResponseStream();
                        if (responseStream != null)
                        {
                            var reader = new StreamReader(responseStream);
    
                            responseMessage = reader.ReadToEnd();
                        }
                    }
                    else
                    {
                        responseMessage = response.StatusDescription;
                    }
                }
                return responseMessage;
            }
    private static byte[] ToByteArrayUsingDataContractSer<T>(T requestBody)
            {
                byte[] bytes = null;
                var serializer1 = new DataContractSerializer(typeof(T));            
                var ms1 = new MemoryStream();            
                serializer1.WriteObject(ms1, requestBody);
                ms1.Position = 0;
                var reader = new StreamReader(ms1);
                bytes = ms1.ToArray();
                return bytes;
            }


    To use the above method i do it using the code below:

     string serviceBaseUrl = "http://localhost/restService";
                string resourceUrl = "/xmldata";
                string method = "POST";
    
                TempData["xml"] = UseHttpWebApproach<string>(serviceBaseUrl, resourceUrl, method, "hello world");


    Now you can monitor your request using Fiddler to see how your request and response would look like.


    Friday, June 29, 2012 3:46 PM