WCF Rest服务没有调用

时间:2014-01-20 09:27:06

标签: c# .net wcf wcf-rest restful-url

我无法呼叫Rest服务。但我能够调用WSDL文件。以下是我的WEB.CONFIG

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

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
        <services>
            <service name ="APWPortalWCFServices.APWPortalWCFService" >
                <endpoint name="RestAPWPortal"
                          address="APWPortalRestful"
                          binding="webHttpBinding"  behaviorConfiguration="APWPortalBehavConfig"
                          contract="APWPortalWCFServices.IAPWPortalContract" >    
                <identity>
                    <dns value="localhost"/>
                </identity>                    
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
        <behaviors>
            <endpointBehaviors>
                <behavior name="APWPortalBehavConfig">
                    <webHttp helpEnabled="True"/>
                </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="false"/>
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    </system.webServer>
</configuration>

的ServiceContract:

namespace APWPortalWCFServices
{
    [ServiceContract]
    public interface IAPWPortalContract
    {
        [OperationContract]
        [WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "GetMyData")]
        Stream GetData();
    }
}

服务实施:

namespace APWPortalWCFServices
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class Service1 : IAPWPortalContract
    {
        public Stream GetData()
        {
            MemoryStream msSubMenuImages = null;
            StringBuilder sbRes = new StringBuilder();

            SqlConnection sqlcon = new SqlConnection("server=192.168.1.2;uid=sa;pwd=xenoDev+eam;database=CinemarkMigration;");
            sqlcon.Open();
            DataSet ds = null;
            SqlDataAdapter sqlda = new SqlDataAdapter("SELECT TOP 10000 * FROM tblHeaderFieldValue", sqlcon);
            sqlda.Fill(ds);

            sbRes.Append(ConvertDatatableToJSON(ds.Tables[0]));

            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
            msSubMenuImages = new MemoryStream(Encoding.UTF8.GetBytes(sbRes.ToString()));
            return msSubMenuImages;
        }
        public string ConvertDatatableToJSON(DataTable dtData)
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = null;
            List<Dictionary<string, object>> rows = null;
            try
            {
                serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                rows = new List<Dictionary<string, object>>();
                Dictionary<string, object> row = null;

                foreach (DataRow dr in dtData.Rows)
                {
                    row = new Dictionary<string, object>();
                    foreach (DataColumn col in dtData.Columns)
                    {
                        if ((col.DataType == typeof(DateTime)) && dr[col] != null && dr[col].ToString().Trim() != string.Empty)
                            row.Add(col.ColumnName, DateTime.Parse(dr[col].ToString()).ToString("MM/dd/yyyy"));
                        else if ((col.DataType == typeof(Decimal)) && dr[col] != null && dr[col].ToString().Trim() != string.Empty)
                            row.Add(col.ColumnName, Decimal.Parse(dr[col].ToString()).ToString("0.00"));
                        else
                            row.Add(col.ColumnName, dr[col]);
                    }
                    rows.Add(row);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return serializer.Serialize(rows);
        }

    }
}

如果我执行此网址:

[http://localhost:50232/APWPortalWCFService.svc/APWPortalRestful/GetMyData] 

它给出了空白页面。我也放了一个调试点。请帮助我

0 个答案:

没有答案