Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between putting WCF namespace or not?

Tags:

c#

asp.net

wcf

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,

  1. Is there any way to resolve this error other than revert the changes? Maybe Web.config is the answer but I'm not sure I'm on the right track.
  2. What is the difference between two [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>
like image 689
Tan Jia Ming Avatar asked Feb 14 '23 04:02

Tan Jia Ming


1 Answers

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.

  1. Not enough information in question to know (but likely issue with configuration).
  2. ServiceContract and ServiceContractAttribute are the same and namespace is not required.
like image 112
PeterB Avatar answered Feb 16 '23 02:02

PeterB