Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 调用函数时发生REST Web服务400错误_C#_Wcf_Web Services_Rest - Fatal编程技术网

C# 调用函数时发生REST Web服务400错误

C# 调用函数时发生REST Web服务400错误,c#,wcf,web-services,rest,C#,Wcf,Web Services,Rest,我制作了一个RESTWeb服务,它与Visual Studio WCF测试客户端一起工作。 可以使用它旁边的网站调用它,并使用使用svcutil创建的“代理”类 当我浏览到.svc文件时,我还可以看到服务在那里。(http://localhost/service1.svc)它会出现“你已经创建了一个服务”的网页 然而 当我尝试调用服务中的函数时(http://localhost/service.svc/firstName/lastName)它返回一个400错误代码 我最近有一个单独的错误,我无法

我制作了一个RESTWeb服务,它与Visual Studio WCF测试客户端一起工作。 可以使用它旁边的网站调用它,并使用使用svcutil创建的“代理”类

当我浏览到.svc文件时,我还可以看到服务在那里。(http://localhost/service1.svc)它会出现“你已经创建了一个服务”的网页

然而

当我尝试调用服务中的函数时(http://localhost/service.svc/firstName/lastName)它返回一个400错误代码

我最近有一个单独的错误,我无法理解,所以我找到了一个关于非常基本的web服务“”的教程,其中一个停止了前面的错误,但在尝试调用函数时仍然有400错误

我的.config文件被缩减到最基本的部分,以便在我进入太远之前让它工作

    <?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="VService">
        <endpoint address="" binding="webHttpBinding" contract="ServiceName.IService1" behaviorConfiguration="webBehavior" />
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webBehavior">
          <webHttp/>
        </behavior>
      </endpointBehaviors>

      <serviceBehaviors>        
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

  <system.diagnostics>
    <trace autoflush="true"  />
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, Error"
              propagateActivity="true">
        <listeners>
          <add name="sdt"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "SdrConfigExample1.e2e" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

</configuration>
函数的代码

namespace VeryBasicService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        public string CallRestService(string firstName, string lastName)
        {
            return firstName + " " + lastName;            
        }      
    }
}

我可以在您的代码中看到两个问题。首先,web.config中
元素的name属性仅列出“serviceName”-如果这不是服务类的完全限定名,则不会使用它。其次,对于GET请求,不应使用
WebInvoke
,而应使用
WebGet

[WebGet(UriTemplate="/{firstname}/{lastname}")]

您的事件日志是否说明发生了什么?您需要打开WCF跟踪,并使用跟踪工具获取更详细的错误。谢谢,请参阅更新:)System.Xml.XmlException:消息正文无法读取,因为它是空的。这是从浏览器端还是服务器端提交的?您可以发布调用该服务的代码吗?如果服务器说客户端正在发送一个空请求,那么问题可能就在这一端。代码来自上面链接的教程。在浏览器中(使用IE 9)”,我将在一秒钟内用服务代码更新上述内容。服务类的完全限定名称是什么?我对web服务非常陌生,所以仍在尝试了解一些事情。也就是最初的WebGet,不幸的是仍然是一样的。一个完全限定的名称将包括名称空间
MyNamespace.ClassName
谢谢。所有信息都有帮助。问题中的代码有轻微的变化,这个名称可以用吗?如果是同位语,则更像是service1是类。或者是.IService1?通过对服务名称和端点契约进行一些操作,我能够为所有内容找到正确的位置。非常感谢。这一点信息对我帮助很大。感谢董先生、雅各布先生、杰瑞德先生和巴布科克先生的帮助:D
namespace VeryBasicService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
        public string CallRestService(string firstName, string lastName)
        {
            return firstName + " " + lastName;            
        }      
    }
}
[WebGet(UriTemplate="/{firstname}/{lastname}")]