MojoDK
MojoDK

Reputation: 4528

WCF and namespaces

I want to create a test WCF service like this: http://www.mywebsite.com/admin/Service1.svc

  1. I create a new project -> WCF -> WCF Service Application
  2. I wrap the automatically created Service1.svc and IService1.vb in "Namespace Admin" like this:

Namespace Admin

Public Class Service1
    Implements IService1

    Public Sub New()
    End Sub

    Public Function GetData(ByVal value As Integer) As String Implements ...
        Return String.Format("You entered: {0}", value)
    End Function

    ' deleted rest of class
End Class

End Namespace

But when I try to add a service reference to Service1.svc, I get this error:

There was an error downloading
'http://localhost:51826/Service1.svc/_vti_bin/ListData.svc/$metadata'.

If I remove my "Namespace Admin" and put Service1.svc into an "Admin" folder, then it works perfectly, but I need to structure my code since this is going to be a large project.

How can I use "Namespace" without it failing?

Upvotes: 0

Views: 547

Answers (2)

Alireza Maddah
Alireza Maddah

Reputation: 5885

When you create a "WCF 4.0 Service Application", it uses the Convention-over-Configuration strategy to configure your web service; One of these conventions is that the implementation of the service is placed inside the "Default Assembly Namespace". To fix this; follow these steps:

  1. Right-click on the .svc file and choose View Markup. This is the XML file that binds the .svc file to the implementation of your service
  2. Change the "Service" attribute value to match the correct location of the service implementation like WcfService1.Admin.Service1
  3. Build the assembly.
  4. Test the service.

Upvotes: 1

YK1
YK1

Reputation: 7612

Namespaces and folders are unrelated. You can have any namespace and put the svc in any folder you desire.

VB provides an implicit root namespace which is your project's name by default. You can see it Project > Properties > Application tab.

Nevertheless, since you want to add a "Admin" namespace, now your class is under second level namespace - <yourProjectName>.Admin.

This has to reflect in your svc file - you have to add "Admin" to the service attribute.

Right click on your svc file in the project and choose > Show markup. Make the change to Service attribute of the ServiceHost tag.

<%@ ServiceHost Language="VB" Debug="true" Service="<YourProjectName>.Admin.Service1" CodeBehind="Service1.svc.vb" %>

Upvotes: 0

Related Questions