我不是网络服务或wcf的新手,但目前我遇到了一个我无法找到解决方案的问题。我尝试了所有我发现搜索堆栈溢出的内容,但没有解决问题。
我正在尝试使用WCF编写一个非常小的服务,该服务应该将结果作为XML,Json或CSV返回。为此,我写了以下代码:
<ServiceContract()>
Public Interface IPortalExchangeService
<OperationContract()> _
<WebGet(BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Xml, UriTemplate:="belegungskalender/xml/{anbieter}/{subD}")> _
Function XMLData(ByVal anbieter As String, ByVal subD As String) As String
<OperationContract()> _
<WebGet(BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="belegungskalender/json/{anbieter}/{subD}")> _
Function JSONData(ByVal anbieter As String, ByVal subD As String) As String
<OperationContract()> _
<WebGet(UriTemplate:="belegungskalender/csv/{anbieter}/{subD}")> _
Function CSVData(anbieter As String, subD As String) As System.IO.Stream
End Interface
现在实现接口的类
Imports System.IO
Public Class CalendarExchangeService
Implements IPortalExchangeService
Public Sub New()
End Sub
Public Function XMLData(ByVal anbieter As String, ByVal subD As String) As String Implements IPortalExchangeService.XMLData
Return String.Format("You entered: {0}", subD)
End Function
Public Function JSONData(ByVal anbieter As String, ByVal subD As String) As String Implements IPortalExchangeService.JSONData
Return String.Format("You entered: {0}", subD)
End Function
Public Function CSVData(ByVal anbieter As String, ByVal subD As String) As Stream Implements IPortalExchangeService.CSVData
Dim ms As New MemoryStream
Dim enc As New UTF8Encoding
Dim arrBytData() As Byte = enc.GetBytes("Hallo;" + anbieter + ";hier;sind;die;Daten;von;" + subD)
ms.Write(arrBytData, 0, arrBytData.Length)
ms.Position = 0
WebOperationContext.Current.OutgoingResponse.ContentType = "text/csv"
Return ms
End Function
End Class
web.config看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="PortalExchangeService" behaviorConfiguration="pesBehavior">
<endpoint address=""
binding="webHttpBinding"
bindingConfiguration=""
behaviorConfiguration="restfulBehavior"
contract="IPortalExchangeService">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restfulBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="pesBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior>
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
最后不仅仅是客户代码:
Dim url As String = "http://localhost:24642/PortalExchangeService.svc/belegungskalender/xml/testanbieter/subDomain"
Dim myRequest As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
myRequest.ContentType = "application/xml"
myRequest.Method = "GET"
Dim myResponse As HttpWebResponse
myResponse = CType(myRequest.GetResponse(), HttpWebResponse)
Dim streamResponse As Stream = myResponse.GetResponseStream()
Dim streamRead As StreamReader = New StreamReader(streamResponse)
Dim readBuffer(255) As Char
Dim count As Integer = streamRead.Read(readBuffer, 0, 256)
Dim result As String = String.Empty
Do While (count > 0)
Dim resultData As String = New String(readBuffer, 0, count)
count = streamRead.Read(readBuffer, 0, 256)
result &= resultData
Loop
streamRead.Close()
streamResponse.Close()
myResponse.Close()
这一切对我来说都很好,但是运行客户端代码我收到的是“400 Bad Request”,行中没有更多信息 “myResponse = CType(myRequest.GetResponse(),HttpWebResponse)”
使用WCF-Client工具测试服务工作正常,但从不使用HTTP GET进行调用。
任何想法我做错了什么?
提前感谢您的提示!!
致以最诚挚的问候,
亨纳尔