locked
WCF service stoped working on migration to MVC4 and VS2013 RRS feed

  • Question

  • User-2146352328 posted

    MVC2 this worked fine(vs2010). See:

    http://forums.asp.net/t/1992971.aspx?Cannot+call+WCF+server+with+Jquery+in+MVC+2

    now it will not call the service. The problem i get is:

    If i put  url: "/wcf/Servicesecurity.svc/securelog" in Ajax i get 404 not found.

    if i put url: url: '<%= @Url.Content("~/wcf/Servicesecurity.svc/securelog")%>', i get 400 bad location and it goes out like this:

    http://localhost:53529/%3C%=%20/wcf/Servicesecurity.svc/securelog%%3E  (on firefox firebug).

    MV4 also complained and made me change the web.config setting for the service, for example from :

    <service name="Servicesecurity" behaviorConfiguration="ServicesecurityBehavior">

    to <service name="MvcApplication1.wcf.Servicesecurity" behaviorConfiguration="ServicesecurityBehavior">

    I tried both was, won't make a difference. Also included the factory directive to the markup of the service.

    So any thoughts?

    P.S. I have registered the script to the view like this:

    @{
        ViewBag.Title = "Home Page";
    }
    @Scripts.Render("~/Scripts/jquery-1.11.1.min.js")
    <script type="text/javascript">
        $(document).ready(function () {
    
           
        });
    
    .....etc
    
    </script>
    
    @section featured {
        <section class="featured">
            <div class="content-wrapper">
    
    ....etc
    

    hope is correct.I tested with an alert in JS and it shows

    I cannot think of anything else i did.So...

    Tuesday, July 1, 2014 7:09 AM

Answers

  • User-417640953 posted

    Hi sapator,

    Thanks for your post.

    Based on your description and code provided, I understand you want to create wcf svc file and works as the rest service in asp.net mvc

    application. For this issue, I think you should set the route first like below.

    //WCF route.........
    routes.IgnoreRoute("WCF/{resource}.svc/{*pathInfo}");
    
    routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Test4", action = "Index", id = UrlParameter.Optional }
                );

    And Service like below.

            [OperationContract]
            [WebInvoke(Method = "GET")]
            public string Test() 
            {
                return "Hello";
            }

    Then config the wcf service in web.config file.

     <service name="MvcApplication.WCF.Service1" behaviorConfiguration="BasicBehavior" >
            <endpoint name="EndPoint1" address="" binding="webHttpBinding" contract="MvcApplication.WCF.IService1" behaviorConfiguration="FirstEndPointBehavoir"  ></endpoint>
          </service>
    
    .........
          <serviceBehaviors>
            <behavior name="BasicBehavior">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" /> 
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="FirstEndPointBehavoir">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>

    Call it in the ajax.

    <script type="text/javascript">
    
            $(document).ready(function () {
    
                $('#Button1').click(function () { 
                    $.ajax({
                        type: "GET",
                        url:'@Url.Content("~/WCF/Service1.svc/Test")' ,
                        contentType: "application/json; charset=utf-8",
                        data: '{}',
                        dataType: "json",
                        success: function (data) {
                            alert(data.d);
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(xhr.status);
                            alert(thrownError);
                        }
                    });
    
                });
            });
    
    </script>

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 1, 2014 11:31 PM

All replies

  • User-2146352328 posted

    Here is the firebug error:

    [HttpException]: The controller for path &#39;/wcf/Servicesecurity.svc/securelog&#39; was not found or does not implement IController.
       at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
       at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
       at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
       at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
       at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
       at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

    Tuesday, July 1, 2014 7:45 AM
  • User-2146352328 posted

    And now, supposedtly i can add and ignore route, like:

     // RouteTable.Routes.IgnoreRoute("wcf/{*.pathInfo}");  //or
                RouteTable.Routes.IgnoreRoute("{wcf}/{Servicesecurity}.svc/{*pathInfo}");

    but i get "NetworkError: 500 Internal Server Error - http://localhost:53529/wcf1/Servicesecurity.svc/securelog"

    {"ExceptionDetail":{"HelpLink":null,"InnerException":{"HelpLink":null,"InnerException":null,"Message":"The OperationFormatter could not deserialize any information from the Message because the Message is empty (IsEmpty = true).","StackTrace":"   at System.ServiceModel.Dispatcher.PrimitiveOperationFormatter.DeserializeRequest(Message message, Object[] parameters)","Type":"System.Runtime.Serialization.SerializationException"},"Message":"Error in deserializing body of request message for operation 'securelog'. The OperationFormatter could not deserialize any information from the Message because the Message is empty (IsEmpty = true)."

    Tuesday, July 1, 2014 8:28 AM
  • User-417640953 posted

    Hi sapator,

    Thanks for your post.

    Based on your description and code provided, I understand you want to create wcf svc file and works as the rest service in asp.net mvc

    application. For this issue, I think you should set the route first like below.

    //WCF route.........
    routes.IgnoreRoute("WCF/{resource}.svc/{*pathInfo}");
    
    routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Test4", action = "Index", id = UrlParameter.Optional }
                );

    And Service like below.

            [OperationContract]
            [WebInvoke(Method = "GET")]
            public string Test() 
            {
                return "Hello";
            }

    Then config the wcf service in web.config file.

     <service name="MvcApplication.WCF.Service1" behaviorConfiguration="BasicBehavior" >
            <endpoint name="EndPoint1" address="" binding="webHttpBinding" contract="MvcApplication.WCF.IService1" behaviorConfiguration="FirstEndPointBehavoir"  ></endpoint>
          </service>
    
    .........
          <serviceBehaviors>
            <behavior name="BasicBehavior">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="false" /> 
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="FirstEndPointBehavoir">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>

    Call it in the ajax.

    <script type="text/javascript">
    
            $(document).ready(function () {
    
                $('#Button1').click(function () { 
                    $.ajax({
                        type: "GET",
                        url:'@Url.Content("~/WCF/Service1.svc/Test")' ,
                        contentType: "application/json; charset=utf-8",
                        data: '{}',
                        dataType: "json",
                        success: function (data) {
                            alert(data.d);
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(xhr.status);
                            alert(thrownError);
                        }
                    });
    
                });
            });
    
    </script>

    Hope this helps, thanks.

    Best Regards!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 1, 2014 11:31 PM
  • User-2146352328 posted

    Will try but i don't want a GET operation but a POST one.

    Wednesday, July 2, 2014 2:27 AM