WCF服务在IOS中不起作用

时间:2014-05-28 07:16:57

标签: ios objective-c xcode web-services wcf

我正在尝试在IOS中使用WCF中的登录Web服务。我使用以下代码来做到这一点。但它不起作用。谁能帮助我做到这一点。

IService代码:

namespace GimbalService
{
[ServiceContract]
public interface IService
{
    [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
       RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.Wrapped,
       UriTemplate = "Register"]
    string Register(string fname, string lname, string email, string password, string country);

    [OperationContract]
    [WebInvoke(Method = "GET",
        ResponseFormat = WebMessageFormat.Json,
        //RequestFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "Login/{email}/{password}"]
    string Login(string email, string password);

    [OperationContract]
    string GetUserGeofence(int userId);

    [OperationContract]
    string GetUserCommunicate(int userId);

    [OperationContract]
    [WebInvoke(Method = "POST",
       ResponseFormat = WebMessageFormat.Json,
       RequestFormat = WebMessageFormat.Json,
       BodyStyle = WebMessageBodyStyle.Wrapped,
       UriTemplate = "CreateUserCommunicate"]
    string CreateUserCommunicate(int communicateId,int userId);

    [OperationContract]
    string DeleteUserGeofence(int userId,int geofenceId);

    [OperationContract]
    string DeleteUserCommunicate(int userId, int communicateId);

    [OperationContract]
    string GetUserGeofenceTypeAndLogo(int geofenceId);

}

}

服务代码是

public string Login(string email, string password)
    {
        XmlConfigurator.Configure();
        GimbalLogger.Instance.Info(String.Format("Incoming Login request: \n Email:{0}", email));
        var userAccessor = new Accessor.Accessor();
        var response = userAccessor.UserLoginAccessor(email, password);
        GimbalLogger.Instance.Info(String.Format("Login Response: {0}", response));
        GimbalLogger.Instance.Info("***********************END OF Login REQUEST***********************";
        return response;
    }

所以,我在这里尝试使用以下Objectice-C代码为IOS应用程序调用此Web服务。

NSArray *propertyNames = [NSArray arrayWithObjects:@"username",@"password", nil];

NSArray *objects = [NSArray arrayWithObjects:txtUserName.text, txtPassword.text, nil];

NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:objects forKeys:propertyNames];

NSData *jsonData = nil;

NSString *jsonString = nil;



NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://192.16.1.255:14/Service.svc/Login"]];

[request setValue:jsonString forHTTPHeaderField:@"json"];

[request setHTTPMethod:@"GET"];

[request setHTTPBody:jsonData];

NSError * errorReturned = nil;

NSURLResponse *theResponse =[[NSURLResponse alloc]init];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];



if(errorReturned)

{

    NSLog(@"%@", errorReturned);

}

else

{

    NSString *retValue = [[NSString alloc] initWithData: data encoding:NSUTF8StringEncoding];

    NSLog(@"%@", retValue);

}

webconfig文件:

<?xml version="1.0"?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate" />
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<connectionStrings>
<add name="AppConnectionString"
       connectionString="Data Source=192.36.12.56;Database=geo;uid=root;pwd=verafghdis;"
       providerName="MySql.Data.MySqlClient"/>
</connectionStrings>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
  <property name="dialect">NHibernate.Dialect.MySqlDialect</property>
  <property name="connection.provider">
    NHibernate.Connection.DriverConnectionProvider
  </property>
  <property name="connection.driver_class">
    NHibernate.Driver.MySqlDataDriver
  </property>
  <property name="connection.connection_string_name">AppConnectionString </property>
  <property name="show_sql">true</property>
  <property name="connection.release_mode">on_close</property>
  <property name="connection.isolation">ReadCommitted</property>
  <property name="command_timeout">300</property>
</session-factory>
</hibernate-configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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>
<protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>    
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="0" />
</system.serviceModel>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1"/>
<customErrors mode="Off"/>
</system.web>
<log4net>
<appender name="GimbalLogFileAppender" type="log4net.Appender.RollingFileAppender">
  <param name="File" value="c:\Dev\Logs\GimbalService\GimbalServiceLog" />
  <param name="AppendToFile" value="true" />
  <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
  <rollingStyle value="Composite" />
  <datePattern value="&quot;_&quot;yyyyMMdd&quot;.log&quot;" />
  <maxSizeRollBackups value="10" />
  <maximumFileSize value="10MB" />
  <layout type="log4net.Layout.PatternLayout">
    <param name="ConversionPattern" value="%-5p%d{yyyy-MM-dd hh:mms} – %m%n" />
    <conversionPattern value="%newline%newline%date %newline%logger [%property{NDC}] %newline&gt;&gt; %message%newline" />
  </layout>
</appender>
<root>
  <level value="DEBUG" />
  <appender-ref ref="GimbalLogFileAppender" />
</root>
</log4net>

<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
    To browse web app root directory during debugging, set the value below to true.
    Set to false before deployment to avoid disclosing web app folder information.
  -->
<directoryBrowse enabled="true"/>
</system.webServer>

</configuration>

我想从Web服务打印值。 提前谢谢。

0 个答案:

没有答案