You need to follow your ABC's:
An address that is exposed through HTTP just needs a deployment URL. Which the Internet Information System is handling all of that for you. However; if you attempt to use the Service Reference
without the proper definitions- it will not show. As it has no details of where it is going.
Once you've defined that criteria it should appear per normal.
Update:
You can culminate SOAP and REST off the same Service Contract or you can separate them. In this example; I'll separate them.
Create the Service:
[ServiceContract]
public interface IMath
{
[OperationContract]
int Add(int Number1, int Number2);
}
Create the Service Contract for Rest:
[ServiceContract]
public interface IMathRest
{
[OperationContract]
[WebGet(UriTemplate = "/Add/{Number1}/{Number2}", RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
int AddRest(string Number1, string Number2);
}
In the above service; it is explicitly setting the message format.
Implement the Service: "Binding"
public class Math : IMath, IMathRest
{
public int Add(int Number1, int Number2)
{
return Number1 + Number2;
}
public int AddRest(string Number1, string Number2)
{
int num1 = Convert.ToInt32(Number1);
int num2 = Convert.ToInt32(Number2);
return num1 + num2;
}
}
Configure the Service:
<serviceBehaviors>
<behavior name = "servicebehavior">
<serviceMetadata httpGetEnabled = "true" />
<serviceDebug includeExceptionDetailInFaults = "false" />
</behavior>
</serviceBehaviors>
The above will set the service to Basic / Web Http.
After you've configured your serviceBehavior
you'll need to define the endpoint:
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
This will configure your Rest Endpoint to the webHttpBinding
specified above. Now you need to define your Soap.
<endpoint name = "SoapEndPoint"
contract = "Namespace in which Service Resides goes here"
binding = "basicHttpBinding" <!-- Mirrors are above configuration -->
address = "soap" />
The above will go into your configuration; however at the time of consumption of the service- the endpoint name is required. The endpoints will be available at the Base Address / Soap Address. The binding used is specified.
Except we have only configured our Soap, not our Rest. So we need to specify our endpoint:
<endpoint name = "RestEndPoint"
contract = "Namespace that our Rest Interface is located goes here"
binding = "webHttpBinding"
address = "rest"
behaviorCOnfiguration = "restBehavior" />
Our Rest Endpoint will be called at our Url (Base Address / Rest/ Add / Parameter / Parameter). We've specified our binds; and set our Rest Behavior.
When you put the whole thing together; it would look like:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name ="servicebehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restbehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name ="MultipleBindingWCF.Service1"
behaviorConfiguration ="servicebehavior" >
<endpoint name ="SOAPEndPoint"
contract ="MultipleBindingWCF.IService1"
binding ="basicHttpBinding"
address ="soap" />
<endpoint name ="RESTEndPoint"
contract ="MultipleBindingWCF.IService2"
binding ="webHttpBinding"
address ="rest"
behaviorConfiguration ="restbehavior"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex" />
</service>
</services>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Consuming:
To consume our Soap it is simple; straight forward. Make the Service Reference and make the call.
static void CallingSoapFunction()
{
SoapClient proxy = new SoapClient("SoapEndPoint");
var result = proxy.Add(7,2); // Proxy opens the channel, we invoke our method, we input our parameters.
Console.WriteLine(result);
}
Except our Restful consumption is slightly different; especially since we have to Format it to Json. So we need to specify the name exactly.
Rest is going to rely on Json for De-serialization of the data.
static void CallRestFunc()
{
WebClient RestProxy = new WebClient();
byte[] data = RestProxy.DownloadData(new Uri("http://localhost:30576/MathRest.svc/Rest/Add/7/2")); // As you see it is following the exact location of the project, invoking method / parameter and so on down the line.
Stream stream = new MemoryStream(data);
DataContractJsonSerializer obj = new DataContractJsonSerializer(typeof(string));
string result = obj.ReadObject(stream).ToString();
Console.WriteLine(result);
}
The above will download the data using the Rest Uri. Deserialize that data; and become displayed.
Hopefully that helps clarify.
If the proper items aren't created in your configuration file; then it will not allow the proper consumption. The ABC's are critical in WCF. If it isn't created in the configuration file you'll need to programatically create them in the code.
Update:
static void Main(string[] args)
{
var binding = new BasicHttpBinding();
var endpoint = new EndpointAddress("http://localhost:8080/");
using (var factory = new ChannelFactory<IPerson>(binding, endpoint))
{
var request = new Dictionary<Guid, Person>();
request[Guid.NewGuid()] = new Person { Name = "Bob", Email = "Bob@abc.com" };
var client = factory.CreateChannel();
var result = client.SetCustomer(request);
Console.WriteLine("Name: {0} | Email: {1}", result.Name, result.Email);
factory.Close();
}
Console.ReadKey(true);
}
As you see in this basic example; the binding and endpoint are both configured. You need to ensure that all of these are defined in both your server and client. It has to know where it is going. Does that make more sense?