客户端在Android环境中使用我的WCF休息服务时获取Method Not Allowed错误

时间:2014-04-25 06:26:58

标签: wcf wcf-ria-services

我有两个post方法返回json结果。一个 update_existing_user 能够由客户端访问,而另一个 submit_reg_data 无法访问。我不明白为什么客户端访问 update_existing_user submit_reg_data 无法访问客户端。但我尝试从邮递员或firefox httpRequester访问这两种方法我得到了完美的结果。

接口:

namespace MFAB
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IMFABService" in both code and config file together.
    [ServiceContract]
    public interface IMFABService
    {
        [WebInvoke(Method = "POST", UriTemplate = "/submit_reg_data",
          ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        [OperationContract]
        Stream submit_reg_data(RegisterProp objResterProperties);

        [WebInvoke(Method = "POST", UriTemplate = "/update_existing_user",
           ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        [OperationContract]
        Stream update_existing_user(RegisterProp RegisterProperties);
}
}

class.svc ---

namespace MFAB
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "MFABService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select MFABService.svc or MFABService.svc.cs at the Solution Explorer and start debugging.

     [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MFABService : IMFABService
    {
        Validation objValidation = new Validation();
        static long intLeadRegisterID = 0;



        public Stream update_existing_user(RegisterProp objRegisterProperties)
        {

            // RegisterProp objRegisterProperties = new RegisterProp();
            // objRegisterProperties = JsonHelper.JsonDeserialize<RegisterProp>(RegisterProperties);
            RegisterBO objRegisterBO = new RegisterBO();
            //RegisterProp objRegisterProp = new RegisterProp();
            DataSet dsDetails = new DataSet();


            Dictionary<string, string> jsonDictionary = new Dictionary<string, string>();
            if (objRegisterProperties.Citizenship <= 0)
            {
                jsonDictionary.Add("SubmissionStatus", "0");
                jsonDictionary.Add("002", "citizenship is required");
                return SendData(jsonDictionary);
            }
            if (objRegisterProperties.LeadRegisterID <= 0)
            {
                jsonDictionary.Add("SubmissionStatus", "0");
                jsonDictionary.Add("002", "LeadRegisterID is required");
                return SendData(jsonDictionary);
            }
            //DataSet objDS = objRegisterBO.VerifyCountryCity(objRegisterProperties);
            //if (objDS != null && objDS.Tables[0].Rows.Count > 0 && Convert.ToBoolean(objDS.Tables[0].Rows[0]["Result"]))
            //{

            objRegisterProperties.LeadStatus = string.Empty;
            jsonDictionary = objValidation.ValidateProperties(objRegisterProperties);
            if (jsonDictionary.Count > 0)
            {

                jsonDictionary.Add("SubmissionStatus", "0");

                return SendData(jsonDictionary);
            }
            else
            {
                //if (objRegisterProperties.DateofBirth.Year > 1900)
                //{
                objRegisterProperties.RegisterID = intLeadRegisterID;
                objRegisterProperties.SPType = "R";
                objRegisterProperties.EnteredDate = DateTime.Now;

                if (Convert.ToBoolean(objRegisterProperties.FamMngBusiness))
                {
                    objRegisterProperties.MngBusiness = true;
                    objRegisterProperties.OwnBusiness = false;
                    objRegisterProperties.OwnCompanyName = string.Empty;
                    objRegisterProperties.OwnAnnualRevenue = string.Empty;
                    objRegisterProperties.OwnOffWebsite = string.Empty;
                    objRegisterProperties.OwnRevCurrencyFormat = 0;
                }
                else
                {
                    objRegisterProperties.MngBusiness = false;
                    if (Convert.ToBoolean(objRegisterProperties.FamOwnBusiness))
                    {
                        objRegisterProperties.OwnBusiness = true;
                        objRegisterProperties.FamMngOffWebsite = string.Empty;
                        objRegisterProperties.RevCurrencyFormat = 0;
                        objRegisterProperties.AnnualRevenue = string.Empty;
                    }
                    else
                    {
                        objRegisterProperties.OwnBusiness = false;
                        objRegisterProperties.OwnCompanyName = string.Empty;
                        objRegisterProperties.OwnAnnualRevenue = string.Empty;
                        objRegisterProperties.OwnOffWebsite = string.Empty;
                        objRegisterProperties.FamMngOffWebsite = string.Empty;
                        objRegisterProperties.OwnRevCurrencyFormat = 0;
                        objRegisterProperties.RevCurrencyFormat = 0;
                        objRegisterProperties.AnnualRevenue = string.Empty;
                    }
                }
                intLeadRegisterID = objRegisterBO.AddRegisterDetails(objRegisterProperties);
                if ((intLeadRegisterID > 0))
                {
                    jsonDictionary.Add("SubmissionStatus", "1");
                    return SendData(jsonDictionary);
                }
                else
                {
                    return ErrorjsonResultData("SubmissionStatus", "0", "010", "Technical Exception");
                }
                //}
                //else
                //{
                //    return ErrorjsonResultData("SubmissionStatus", "0", "052", "Enter valid DateofBirth");
                //}
            }

            //}
            //else
            //{
            //    return ErrorjsonResultData("SubmissionStatus", "0", "002", "Please select the city from the displayed options");
            //}


        }

        public Stream submit_reg_data(RegisterProp objRegisterProperties)
        {
            //RegisterProp objRegisterProperties = new RegisterProp();
            //objRegisterProperties = JsonHelper.JsonDeserialize<RegisterProp>(RegisterProperties);
            RegisterBO objRegisterBO = new RegisterBO();
            //RegisterProp objRegisterProp = new RegisterProp();
            DataSet dsDetails = new DataSet();
            bool chkStatus = true;


            dsDetails = objRegisterBO.FetchRegisterDetailsByEmailIDs(objRegisterProperties);
            if (!(dsDetails == null))
            {
                if ((dsDetails.Tables[0].Rows.Count > 0))
                {
                    chkStatus = false;
                }
            }
            if (chkStatus)
            {
                Dictionary<string, string> jsonDictionary = new Dictionary<string, string>();
                //DataSet objDS = objRegisterBO.VerifyCountryCity(objRegisterProperties);
                //if (objDS != null && objDS.Tables[0].Rows.Count > 0 && Convert.ToBoolean(objDS.Tables[0].Rows[0]["Result"]))
                //{
                objRegisterProperties.LeadStatus = string.Empty;
                jsonDictionary.Add("SubmissionStatus", "0");
                jsonDictionary = objValidation.ValidateProperties(objRegisterProperties);
                if (jsonDictionary.Count > 0)
                {
                    return SendData(jsonDictionary);
                }
                else
                {
                    jsonDictionary.Clear();
                    //if (objRegisterProperties.DateofBirth.Year > 1900)
                    //{
                    objRegisterProperties.RegisterID = intLeadRegisterID;
                    objRegisterProperties.SPType = "R";
                    objRegisterProperties.EnteredDate = DateTime.Now;
                    if (Convert.ToBoolean(objRegisterProperties.FamMngBusiness))
                    {
                        objRegisterProperties.MngBusiness = true;
                        objRegisterProperties.OwnBusiness = false;
                        objRegisterProperties.OwnCompanyName = string.Empty;
                        objRegisterProperties.OwnAnnualRevenue = string.Empty;
                        objRegisterProperties.OwnOffWebsite = string.Empty;
                        objRegisterProperties.OwnRevCurrencyFormat = 0;
                    }
                    else
                    {
                        objRegisterProperties.MngBusiness = false;
                        if (Convert.ToBoolean(objRegisterProperties.FamOwnBusiness))
                        {
                            objRegisterProperties.OwnBusiness = true;
                            objRegisterProperties.FamMngOffWebsite = string.Empty;
                            objRegisterProperties.RevCurrencyFormat = 0;
                            objRegisterProperties.AnnualRevenue = string.Empty;
                        }
                        else
                        {
                            objRegisterProperties.OwnBusiness = false;
                            objRegisterProperties.OwnCompanyName = string.Empty;
                            objRegisterProperties.OwnAnnualRevenue = string.Empty;
                            objRegisterProperties.OwnOffWebsite = string.Empty;
                            objRegisterProperties.FamMngOffWebsite = string.Empty;
                            objRegisterProperties.OwnRevCurrencyFormat = 0;
                            objRegisterProperties.RevCurrencyFormat = 0;
                            objRegisterProperties.AnnualRevenue = string.Empty;
                        }
                    }
                    intLeadRegisterID = objRegisterBO.AddRegisterDetails(objRegisterProperties);
                    if ((intLeadRegisterID > 0))
                    {
                        jsonDictionary.Add("SubmissionStatus", "1");
                        jsonDictionary.Add("LeadRegisterId", intLeadRegisterID.ToString());
                        return SendData(jsonDictionary);
                    }
                    else
                    {
                        return ErrorjsonResultData("SubmissionStatus", "0", "010", "Technical Exception");
                    }
                    //}

                    //else
                    //{
                    //    return ErrorjsonResultData("SubmissionStatus", "0", "052", "Enter valid DateofBirth");
                    //}

                }
                //}
                //else
                //{
                //    return ErrorjsonResultData("SubmissionStatus", "0", "003", "Please select the city from the displayed options");
                //}
            }
            else
            {
                return ErrorjsonResultData("SubmissionStatus", "0", "003", "A record with this email or mobile number already exists!!");
            }
        }
}}

web.config文件---&gt;

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <connectionStrings>
    <add name="DBConnStr" connectionString="server=inhydxyxertt;database=xyztre;uid=sa;password=123tgr;" providerName="System.Data.SqlClient" />
  </connectionStrings>

  <appSettings>
    <add key="AppYear" value="2014" />
    <add key="ApplYear" value="2014-15" />
    <!--<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />-->
  </appSettings>

  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <bindings>
      <webHttpBinding>
        <binding name="webBinding" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </webHttpBinding>

    </bindings>
    <services>
      <service behaviorConfiguration="MFAB.UserServiceBehavior" name="MFAB.MFABService">
        <endpoint address="" behaviorConfiguration="MFAB.UserServiceBehavior"  binding="webHttpBinding" bindingConfiguration="webBinding" contract="MFAB.IMFABService"/>


          <!--<identity>
            <dns value="localhost" />
          </identity>-->


        <!--<endpoint address="/Submit" behaviorConfiguration="MFAB.UserServiceBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="MFAB.IMFABService1"/>-->



        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="MFAB.UserServiceBehavior">
          <webHttp helpEnabled="true" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MFAB.UserServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"  minFreeMemoryPercentageToActivateService="0" />
  </system.serviceModel>

 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
        <directoryBrowse enabled="true" showFlags="Date, Time, Size, Extension, LongDate" />
        <defaultDocument>
            <files>
                <add value="MFABService.svc" />
            </files>
        </defaultDocument>
  </system.webServer>

</configuration>

Global.aspx ---

    namespace MFAB
    {
        public class Global : System.Web.HttpApplication
        {

            protected void Application_Start(object sender, EventArgs e)
            {
               RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(MFABService)));
            }

            protected void Session_Start(object sender, EventArgs e)
            {

            }

            protected void Application_BeginRequest(object sender, EventArgs e)
            {

                HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                HttpContext.Current.Response.Cache.SetNoStore();

                EnableCrossDmainAjaxCall();
            }

            protected void Application_AuthenticateRequest(object sender, EventArgs e)
            {

            }

            protected void Application_Error(object sender, EventArgs e)
            {

            }

            protected void Session_End(object sender, EventArgs e)
            {

            }

            protected void Application_End(object sender, EventArgs e)
            {

            }

            private void EnableCrossDmainAjaxCall()
            {
                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin","*");

                if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
                {
                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
                                  "GET, POST");
                    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
                                  "Content-Type, Accept");
                    HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
                                  "1728000");
                    HttpContext.Current.Response.End();
                }
            }
        }
    }

0 个答案:

没有答案