I was following this tutorial, and also came across this article after I finished the previous tutorial.
What makes me wonder is the [ServiceContractAttribute]
. I saw the [ServiceContract]
in the article does not have Namespace, but the tutorial has one.
So I went ahead and changed [ServiceContract(Namespace="SandwichServices")]
into [ServiceContract]
, but when I run the application and click the button, I get an exception: Uncaught ReferenceError: SandwichServices is not defined
.
So I would like to know,
[ServiceContractAttribute]
? From my perspective, looks like Namespace
is not required for interfaces, but am I right?Web.config file contents:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="SandwichServices.CostServiceAspNetAjaxBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
<services>
<service name="SandwichServices.CostService">
<endpoint address=""
behaviorConfiguration="SandwichServices.CostServiceAspNetAjaxBehavior"
binding="webHttpBinding"
contract="SandwichServices.CostService" />
</service>
</services>
</system.serviceModel>
You are correct, the namespace property of the ServiceContractAttribute is not required in your contract definition, but it defaults to "http://tempuri.org". This is used to define the namespace of the port type in the WSDL. It is unclear from your question why the error is occurring.
It is good practice (particularly for externally facing APIs) to use a non-default namespace in urn format (e.g., urn:companyname:servicename). Additionally, you can use the Name property to further define the service.
Example:
For a menu service
[ServiceContract(Name="menu", Namespace="urn:subway:sandwich")]
For an order service
[ServiceContract(Name="order", Namespace="urn:subway:sandwich")]
etc.
And typically, you would match the WSDL namespace to the CLR namespace in your code.
To conintue the example:
namespace Subway.Sandwich
{
[ServiceContract(Name="menu", Namespace="urn:subway:sandwich")]
public interface MenuService
{
}
[ServiceContract(Name="order", Namespace="urn:subway:sandwich")]
public interface OrderService
{
}
}
To answer your specific questions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With