How to prevent to navigate to same state again.(angular JS)

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

Use “$stateChangeStart” event

Use below code for same.Just check your current state and the state where you are supposed to navigate.
angular.module(‘app’).run([‘$rootScope’,’$state’, function ($rootScope,$state) {
$rootScope.$on(‘$stateChangeStart’,function (event, toState, toParams, fromState, fromParams, error){
if ((fromState.name == “SandipPatilHomeState“) && (toState.name == “SandipPatilHomeState“)) {
event.preventDefault();
return $state.go(“SandipPatilHomeState“);
}
return;
});
}

 

Note: event.preventDefault(); Will keeps you on current state

 

bz

 

 

Advertisements

How to invoke WCF service method which is deployed at some other server from webapi controller in any application Approach-2

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

Step 1)

Let’s see created WCF service

service web.config for it like

j2

<service behaviorConfiguration=”HttpGetMetadata” name=”SP.Phoenix.Service.AdminDataService”>
<endpoint address=”AdminDataService” binding=”basicHttpBinding” bindingConfiguration=”BasicHttpBinding” contract=”SP.Phoenix.Service.IAdminDataService”/>
<endpoint address=”” binding=”basicHttpBinding” bindingConfiguration=”BasicHttpBinding” contract=”SP.Phoenix.Service.IAdminDataService”/>
<endpoint address=”mex” binding=”mexHttpBinding” bindingConfiguration=”” contract=”IMetadataExchange”/>
<host>
<baseAddresses>
<add baseAddress=”http://localhost:9055/”/&gt;
</baseAddresses>
</host>
</service>

 

Just look at WCF service

 

namespace SP.Phoenix.Service
{
[ServiceContract]
public interface IAdminDataService
{

}

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class AdminDataService : IAdminDataService
{

}

 

Now let’s see how to consume it into WebApi controller

 

Web.config for webapi project

j1.png

<endpoint address=”http://ServerName/WebService/AdminDataService.svc/AdminDataService&#8221;
binding=”basicHttpBinding” bindingConfiguration=”BasicHttpBinding_IAdminDataService”
contract=”AdminDataService.IAdminDataService” name=”BasicHttpBinding_IAdminDataService” />
</client>

 

Now go to webapi project and add service reference as shown and give name AdminDataService

j3.png

 

once reference is added

let’s look into reference.cs and copy content from this file and create your own class with name

AdminDataService.cs 

and paste into it.

Now delete all references for this added service including reference.cs

Now use this

AdminDataService class instance to invoke methods inside it.

 

Now use it in WebAPI Controller like as shown below

j4.png

 

Now remember AdminDataService.cs contains elements like

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute(“System.ServiceModel”, “4.0.0.0”)]
public partial class AdminDataServiceClient : System.ServiceModel.ClientBase<APIServer.AdminDataService.IAdminDataService>, APIServer.AdminDataService.IAdminDataService
{

public AdminDataServiceClient()
{
}

public AdminDataServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}

public AdminDataServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}

public AdminDataServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}

public AdminDataServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}

}

 

 

 

How to invoke WCF service method which is deployed at some other server from webapi controller in any application Approach-1

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

Step 1)

Let’s see created WCF service

k1

 

namespace SP.Web.Services
{
[ServiceContract]
public interface IPatientsJSONService
{
[OperationContract]
List<PatientDTO> GetPatientsBySearchExtendedCriteria(PatientSearchDTO searchCriteria);

[OperationContract]
PatientEntityDTO GetPatientEntityById(Guid patientId);
}
public class PatientsJSONService : IPatientsJSONService
{
private log4net.ILog _logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string _org = ConfigurationManager.AppSettings[“crm.organizationName”];

[WebInvoke(Method = “POST“, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
public List<PatientDTO> GetPatientsBySearchExtendedCriteria([FromBody]PatientSearchDTO searchCriteria)
{
//Your Logic
}
}

 

step 2) Let see service web.config for it

 

k2

 

<services>

<service behaviorConfiguration=”webHttpGetMetadata” name=”SP.Web.Services.PatientsJSONService”>
<endpoint binding=”webHttpBinding” contract=”SP.Web.Services.IPatientsJSONService” behaviorConfiguration=”Web” />
</service>
</services>

 

Step 3) let’s see how to invoke it from any applications webapi controller

Just observe how parameter is used for service method

 

k3

 

[Authorize]
public HttpResponseMessage Post([FromBody]PatientSearchNetworkParams postDTO)
{
HttpResponseMessage resp;
string orignalContent = “”;
PatientSearchDTO patientSearchDTO = new PatientSearchDTO();
string param= JsonConvert.SerializeObject(patientSearchDTO , new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat });
string result = string.Empty;
var request = (HttpWebRequest)WebRequest.Create(http://YourserverName/ISV/SP.web.services/PatientsJSONService.svc/GetPatientsBySearchExtendedCriteria);
if (request != null)
{
request.ContentType = “application/json”;
request.Method = “POST”;
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
byte[] requestBodyBytes = GetBytes(param);
request.ContentLength = requestBodyBytes.Length;
System.IO.Stream postStream = request.GetRequestStream();
postStream.Write(requestBodyBytes, 0, requestBodyBytes.Length);
postStream.Flush();
postStream.Close();
var response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
System.IO.Stream responseStream = response.GetResponseStream();
if (responseStream != null)
{
var reader = new System.IO.StreamReader(responseStream);

result = reader.ReadToEnd();
}
}
}
resp = Request.CreateResponse(HttpStatusCode.OK, result );
return resp;
}

 

 

Solution for: HTTP Error 404.3 – Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler.

(In order to Enlarge images Please Right Click and Open images in New Tab).

dq

I fixed on Windows 10:

  1.  Control Panel – Programs and Features – Turn Windows features on or off.
  2. Select .NET Framework 4.6 Advanced Services – WCF Services
  3. Check HTTP Activation.
  4. OK done.
  5. Please find below screenshot.

dq1

how to replace “\” with “_” in string in c#.net

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

2

Let’s see how to replace “\” with “_” in string in c#.net

Since it’s not straight forward by using String.Replace method.

So let’s take move ahead with it

suppose your string item is like that
string filepath=”C:\\\\DemoFolder\\Claims”;

now use below code

filepath.Replace(@”\”, @”_”);

Output=>  “C:__DemoFolder_Claims”

 

How to check File/Directory Access for particular user and allow that user to read/Write into that directory.

(In order to Enlarge images Please Right Click and Open images in New Tab).

 

2

Let’s see How to check File/Directory Access for particular user and allow that user to read/Write into that directory.

 

In order to do this perform below steps

 

Step-1)

Add this class in your project with name NetworkConnection

 

public class NetworkConnection : IDisposable
{
string _networkName;

public NetworkConnection(string networkName,
NetworkCredential credentials)
{
_networkName = networkName;

var netResource = new NetResource()
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};

var result = WNetAddConnection2(
netResource,
credentials.Password,
credentials.UserName,
0);

if (result != 0)
{
//throw new Win32Exception(result, “Error connecting to remote share”);
}
}

~NetworkConnection()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
WNetCancelConnection2(_networkName, 0, true);
}

[DllImport(“mpr.dll”)]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);

[DllImport(“mpr.dll”)]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
}

Step-2) also add this class as well NetResource

[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}

public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};

public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}

public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}

 

Step-3)

Let us see how to use it in order to check access for particular user with domain name and user  credentials:

NetworkCredential theNetworkCredential = new NetworkCredential(“username”, “password”, “domainName”);

//Here NetworkCredential  it’s from System.Net;

Now use it like

using (new NetworkConnection(FilePath , theNetworkCredential))
{

//your code goes here where you can perform operations on File
//FilePath should be with filename into it

byte[] fileData = Convert.FromBase64String(FileBinaryData);
Stream stream = new MemoryStream(fileData);

//Below code is for writing into file
using (new NetworkConnection(FilePath , theNetworkCredential))
{
File.WriteAllBytes(FilePath + item.FileName, fileData);
}
//OR

using (Stream file = File.Create(FilePath , fileData.Length, FileOptions.WriteThrough))
{
stream.CopyTo(file);

}

//Below code is for reading from file
byte[] fileBinary = null;
fileBinary = File.ReadAllBytes(FilePath);

//Use this fileBinary for rendering on UI

}

 

                                          OR

using ( new Impersonator( "YourUsername", "YourDomainname", "YourPassword" ) )
{
   //Here your all code goes which is going to perform I/O on
   // Files/Directories
//Which avoids all access path exceptions
}

 

add this class

 

refer:

https://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User