tags:

views:

13

answers:

0

Hi all

I am new to WCF, I was using web service asmx before. I have trouble on making my wcf JSON ajax service work through http like asmx.Could you help me see what wrong in my code?

My WCF services are defined in my website application folder. My aim is to call this service in my aspx page java-script code and return complex object back as JSON to my javascript. I have no problem on doing this through classic asmx web service.

When I try to test it through my browser by type this in the URL , URL-http://localhost:3849/WebServices/Account/WCFCompanyService.svc/GetAll. it return "Method not allowed."

But if I type just this URL-http://localhost:3849/WebServices/Account/WCFCompanyService.svc, it return this:

WCFCompanyService Service

You have created a service.

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

svcutil.exe URL-http://localhost:3849/WebServices/Account/WCFCompanyService.svc?wsdl

This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:

C#

class Test { static void Main() { HelloClient client = new HelloClient();

// Use the 'client' variable to call operations on the service.

// Always close the client.
client.Close();   } }

This is my Ajax-enabled WCF service code:

 [ServiceContract(Namespace = "")]
  [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
  public class WCFCompanyService
  {
    // To use HTTP GET, add [WebGet] attribute. (Default ResponseFormat is WebMessageFormat.Json)
    // To create an operation that returns XML,
    //   add [WebGet(ResponseFormat=WebMessageFormat.Xml)],
    //   and include the following line in the operation body:
    //     WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
    [OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json)]
    public List<CompanyDTO> GetAll()
    {

        CustomerWebPortal_BLL2.Account.CompanyBO com = new CustomerWebPortal_BLL2.Account.CompanyBO();
        return com.GetCompanies().Select(p => new CompanyDTO { CompanyName = p.CompanyName, ID = p.ID }).ToList();


    }

    // Add more operations here and mark them with [OperationContract]
  }

This is the web.config service model setting

<system.serviceModel>
  <behaviors>
   <serviceBehaviors>
    <behavior name="CustomerWebPortal.WebServices.Account.WCFCompanyServiceAspNetAjaxBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
   </serviceBehaviors>
   <endpointBehaviors>
    <behavior name="CustomerWebPortal.WebServices.Account.WCFCompanyServiceAspNetAjaxBehavior">
     <webHttp />
    </behavior>
   </endpointBehaviors>
  </behaviors>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
   multipleSiteBindingsEnabled="true" />
  <services>
   <service behaviorConfiguration="CustomerWebPortal.WebServices.Account.WCFCompanyServiceAspNetAjaxBehavior" name="CustomerWebPortal.WebServices.Account.WCFCompanyService">
    <endpoint address="" behaviorConfiguration="CustomerWebPortal.WebServices.Account.WCFCompanyServiceAspNetAjaxBehavior"
     binding="webHttpBinding" contract="CustomerWebPortal.WebServices.Account.WCFCompanyService" />
   </service>
  </services>
 </system.serviceModel>

Regards

Bryan