Reputation: 1899
I'm a beginner in WCF and have created a RESTful service called OrderProcessor with three operations:
bool IsClientActive(string token);
Order ProcessOrder();
string CheckStatus(Guid orderNumber);
I require suggestions and feedback on few points related to the same service:
1. Attribute Routing: I know that like in WebAPI, Attribute Routing is not possible in WCF, but I want to create the api with following URLs:
http://localhost:{portnumber}/OrderProcessor/IsClientActive/{token} - POST request for IsClientActive() method
http://localhost:{portnumber}/OrderProcessor/ProcessOrder - GET request for the ProcessOrder() method
http://localhost:{portnumber}/OrderProcessor/CheckStatus/{orderNumber} - POST request for the CheckStatus() method
So, I have defined the Interface and implementation of the service as follows:
Contract - IOrderProcessor.cs
interface IOrderProcessor
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api/{token}")]
bool IsClientActive(string token);
[OperationContract(IsOneWay = false)]
[WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api")]
Order ProcessOrder();
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api/{orderNumber}")]
string CheckStatus(Guid orderNumber);
}
Implementation - OrderProcessor.cs
public class OrderProcessor : IOrderProcessor
{
public bool IsClientActive(string token)
{
bool status = false;
try
{
if (!string.IsNullOrEmpty(token.Trim()))
{
//Do db checking
status = true;
}
status = false;
}
catch (Exception ex)
{
//Log exception
throw ex;
}
return status;
}
public Order ProcessOrder()
{
Order newOrder = new Order()
{
Id = Guid.NewGuid(),
Owner = "Admin",
Recipient = "User",
Info = "Information about the order",
CreatedOn = DateTime.Now
};
return newOrder;
}
public string CheckStatus(Guid orderNumber)
{
var status = string.Empty;
try
{
if (!(orderNumber == Guid.Empty))
{
status = "On Track";
}
status = "Order Number is invalid";
}
catch (Exception)
{
//Do logging
throw;
}
return status;
}
}
Web.config
<system.serviceModel>
<services>
<service name="WCF_MSMQ_Service.OrderProcessor" behaviorConfiguration="ServiceBehavior">
<!-- Service Endpoints -->
<host>
<baseAddresses>
<add baseAddress="http://localhost:4723/"/>
</baseAddresses>
</host>
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="" binding="webHttpBinding" contract="WCF_MSMQ_Service.IOrderProcessor" behaviorConfiguration="Web"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- Enable metadata publishing. -->
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true.
Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Issues: I have implemented all the code but when i try to run it (View in Browser) using Visual Studio, I'm not able to access the above defined URLs. For example, I tried to check the URL: http://localhost:4723/OrderProcessor/api it is throwing the following error:
In contract 'IOrderProcessor', there are multiple operations with Method 'POST' and a UriTemplate that is equivalent to '/api/{orderNumber}'. Each operation requires a unique combination of UriTemplate and Method to unambiguously dispatch messages. Use WebGetAttribute or WebInvokeAttribute to alter the UriTemplate and Method values of an operation.
I tried to search for this error and someone suggested to put "[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]" on the imple,mentation class, but the error is still here [AddressFilter mismatch at the EndpointDispatcher - the msg with To. Can someone please suggest a way to use URLs just like the WebAPI way?
Upvotes: 3
Views: 1069
Reputation: 2178
Simply UriTemplate for both your below service methods are not distinguishable,
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api/{token}")]
bool IsClientActive(string token);
and
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/api/{orderNumber}")]
string CheckStatus(Guid orderNumber);
To distinguish you can change it as below by adding method names in UriTemplate
UriTemplate = "/api/isClientActive/{token}"
UriTemplate = "/api/checkStatus/{orderNumber}"
Upvotes: 2